23 questions · 130 marks · 8 papers. Try each one before checking.
Solve them and check answers →Read the notes first
2023 Aug
Question 3 MCQ · 3 marks
Consider the Java code given below.
public class Example{
public <T extends Comparable> void sortArray(T[] obj){
// Sorts obj
}
public <T> void elementDisplay(T[] arr){
// Displays the elements of arr
}
public <T extends Number> T sum(List<T> lst){
// Returns the sum of elements of lst
}
}
How does class Example look after type erasure?
public class Example{
public void sortArray(Object[] obj){
// Sorts obj
}
public void elementDisplay(Object[] arr){
// Displays the elements of arr
}
public Object sum(List<Object> lst){
// Returns the sum of elements of lst
}
}
public class Example{
public void sortArray(Comparable[] obj){
// Sorts obj
}
public void elementDisplay(Object[] arr){
// Displays the elements of arr
}
public Number sum(List<Number> lst){
// Return the sum of elements of lst
}
}
public class Example{
public void sortArray(Comparable[] obj){
// Sorts obj
}
public void elementDisplay(Object[] arr){
// Displays the elements of arr
}
public T sum(List<Number> lst){
// Return the sum of elements of lst
}
}
public class Example{
public void sortArray(T[] obj){
// Sorts obj
}
public void elementDisplay(T[] arr){
// Displays the elements of arr
}
public T sum(List<T> lst){
// Return the sum of elements of lst
}
}
Question 11 MSQ · 4 marks
Consider the Java code given below that prints the salaries of employees and managers. From among the options, identify the appropriate function header for function printSalary that takes as input a list of employees and managers, and prints their salaries.
import java.util.*;
class Employee{
double salary;
public Employee(double s){
salary = s;
}
public double getSalary(){
return salary;
}
}
class Manager extends Employee{
public Manager(double s){
super(s);
}
}
class Test {
// FUNCTION HEADER for function printSalary
{
for(int i = 0; i < lst.size(); i++){
System.out.println(lst.get(i).getSalary());
}
}
public static void main(String[] args) {
List<Employee> e = new ArrayList<Employee>();
e.add(new Employee(12000));
e.add(new Employee(2000));
List<Manager> m = new ArrayList<Manager>();
m.add(new Manager(13000));
m.add(new Manager(1200));
printSalary(e);
printSalary(m);
}
}
Choose the correct option(s).
public static void printSalary(List<Employee> lst)
public static <T extends Employee> void printSalary(List<T> lst)
public static void printSalary(List<? extends Employee> lst)
public static void printSalary(List<Manager> lst)
Question 12 MSQ · 4 marks
Consider the Java code given below that prints the highest payment charge among a set of given PaymentApp objects. From among the options, identify the appropriate function header for function printHighCharge that takes as input an array of PaymentApp objects and prints the highest charge.
import java.util.*;
interface PaymentApp {
public abstract double paymentCharges();
}
class XPay implements PaymentApp{
double payAmount;
// Constructor
// method paymentCharges() that returns 5% of payAmount
}
class YPay extends XPay{
// Constructor
// method paymentCharges() that returns 6% of payAmount
}
public class Test{
// LINE 1: FUNCTION HEADER
{
// invokes method paymentCharges()
// to print the value of highest payment charge
}
public static void main(String[] args) {
PaymentApp[] p = {new XPay(1200), new YPay(1200)};
printHighCharge(p);
}
}
Choose the correct option(s).
public static void printHighCharge(<?> p)
public static <T extends PaymentApp> void printHighCharge(T[] p)
public static <T extends XPay> void printHighCharge(T[] p)
public static void printHighCharge(PaymentApp[] p)
2023 Dec
Question 10 MCQ · 4 marks
Consider the Java code given below.
public class ListOperations {
public <E> int countOccurrences(E[] arr, E element) {
// Counts the number of occurrences of the element in the array
}
public <E extends Comparable<E>> void sort(E[] arr) {
// Sorts the array
}
}
How does class ListOperations look after type erasure?
public class ListOperations {
public int countOccurrences(Object[] arr, Object element) {
// Counts the number of occurrences of the element in the array
}
public void sort(Object[] arr) {
// Sorts the array
}
}
public class ListOperations {
public int countOccurrences(Object[] arr, Object element) {
// Counts the number of occurrences of the element in the array
}
public void sort(Comparable[] arr) {
// Sorts the array
}
}
public class ListOperations {
public int countOccurrences(Object[] arr, Object element) {
// Counts the number of occurrences of the element in the array
}
public void sort(E[] arr) {
// Sorts the array
}
}
public class ListOperations {
public int countOccurrences(E[] arr, E element) {
// Counts the number of occurrences of the element in the array
}
public void sort(E[] arr) {
// Sorts the array
}
}
Question 13 MSQ · 3 marks
Consider the Java code given below that prints the highest score among a set of given Scorable objects. From among the options, identify the appropriate function header for the function printHighestScore that takes as input an array of Scorable objects and prints the highest score.
import java.util.*;
interface Scorable {
public abstract int getScore();
}
public class Student implements Scorable {
private int score;
// Constructor
// method getScore() that returns score of Student
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getScore()
// to print the value of highest score
}
public static void main(String[] args) {
Scorable[] scores = {new Student(85), new Student(92), new Student(78)};
printHighestScore(scores);
}
}
Choose the correct option(s).
public static void printHighestScore(<?> items)
public static <T extends Scorable> void printHighestScore(T[] items)
public static <T extends Student> void printHighestScore(T[] items)
public static void printHighestScore(Scorable[] items)
Question 15 MSQ · 4 marks
Consider the Java code given below that prints the price of cars. From among the options, identify the appropriate function header for the function printPrice that takes as input a list of cars and prints their prices.
import java.util.*;
class Car{
private double price;
public Car(double p){
price = p;
}
public double getPrice(){
return price;
}
}
class Hyundai extends Car{
public Hyundai(double p) {
super(p);
}
}
class Renault extends Car{
public Renault(double p) {
super(p);
}
}
public class Test {
// FUNCTION HEADER for function printPrice
{
for(int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i).getPrice());
}
}
public static void main(String[] args) {
List<Hyundai> h = new ArrayList<Hyundai>();
h.add(new Hyundai(1.30));
h.add(new Hyundai(1.20));
List<Renault> r = new ArrayList<Renault>();
r.add(new Renault(2.30));
r.add(new Renault(5.60));
printPrice(h);
printPrice(r);
}
}
Choose the correct option(s).
public static void printPrice(List<Car> lst)
public static void printPrice(List<Renault> lst)
public static void printPrice(List<? extends Car> lst)
public static void printPrice(List<Object> lst)
2024 Mar 24
Question 11 MCQ · 7 marks
Consider the Java code given below.
public class ArrayUtils{
public <T extends Comparable> T max(T[] arr){
// code for finding maximum here
}
public <T extends Number> T avg(T[] arr){
// code for finding average of elements here
}
public <T> int count(T[] arr){
// code for counting the number of elements in array
}
}
How does class ArrayUtils look after type erasure?
public class ArrayUtils{
public Object max(Object[] arr){
// code for finding maximum here
}
public Number avg(Number[] arr){
// code for finding average of elements here
}
public int count(Object[] arr){
// code for counting the number of elements in array
}
}
public class ArrayUtils{
public Object max(Object[] arr){
// code for finding maximum here
}
public Object avg(Object[] arr){
// code for finding average of elements here
}
public int count(Object[] arr){
// code for counting the number of elements in array
}
}
public class ArrayUtils{
public Comparable max(Comparable[] arr){
// code for finding maximum here
}
public Number avg(Number[] arr){
// code for finding average of elements here
}
public int count(Object[] arr){
// code for counting the number of elements in array
}
}
public class ArrayUtils{
public Comparable max(Comparable[] arr){
// code for finding maximum here
}
public Integer avg(Integer[] arr){
// code for finding average of elements here
}
public int count(Object[] arr){
// code for counting the number of elements in array
}
}
Question 2 MCQ · 6 marks
Consider the Java code given below.
The method boolean containsKey(Object key) in the class Map returns true if and only if the map contains a mapping for a key k such that Objects.equals(key, k).
import java.util.*;
interface Shape {
void draw();
}
class Circle implements Shape {
public void draw() {
System.out.println("Drawing a Circle");
}
}
class Square implements Shape {
public void draw() {
System.out.println("Drawing a Square");
}
}
class DrawingBoard<T extends Shape> {
private Map<String, T> m = new HashMap<>(); // LINE 1
public void add(String name, T shape) {
m.put(name, shape);
}
public void draw(String name) {
if (m.containsKey(name)) {
T s = m.get(name); //LINE 2
s.draw();
} else {
System.out.println("Shape not found");
}
}
}
public class Test {
public static void main(String[] args) {
DrawingBoard<Shape> dB = new DrawingBoard<Shape>();
Shape s1 = new Circle();
Shape s2 = new Square();
dB.add("circle", s1);
dB.add("square", s2);
dB.draw("circle");
dB.draw("triangle");
}
}
Choose the correct option.
LINE 1 generates compilation error because type T is not known
This program generates output:
Drawing a Circle
LINE 2 generates compilation error because a variable of type Shape cannot refer to objects of type Circle and Square
This program generates output:
Drawing a Circle
Shape not found
Question 15 MSQ · 8 marks
Consider the Java code given below that prints the highest goals among a set of given GoalScorer objects. From among the options, identify the appropriate function header for the function printHighestGoals that takes as input an array of GoalScorer objects and prints the highest goal.
import java.util.*;
interface GoalScorer {
public abstract int getGoals();
}
public class Player implements GoalScorer {
private double goals;
// Constructor
// method getGoals() that returns goals
}
public class Test {
// LINE 1: FUNCTION HEADER
{
// invokes method getGoals()
// to print the value of highest goals
}
public static void main(String[] args) {
GoalScorer[] players = {new Player(123), new Player(98), new Player(79)};
printHighestGoals(players);
}
}
Choose the correct option(s).
public static void printHighestGoals(<?> players)
public static <T extends GoalScorer> void printHighestGoals(T[] players)
public static <T extends Player> void printHighestGoals(T[] players)
public static void printHighestGoals(GoalScorer[] players)
2024 Aug 04
Question 10 MCQ · 7 marks
Consider the Java code given below.
public class ArrayOperations {
public <T> void reverse(T[] array) {
// Reverses the order of elements in the array
}
public <T extends Number> double sum(T[] array) {
// Calculates the sum of elements in the array
}
}
After type erasure, which of the following correctly represents the ArrayOperations class?
public class ArrayOperations {
public void reverse(Object[] array) {
// Reverses the order of elements in the array
}
public double sum(Number[] array) {
// Calculates the sum of elements in the array
}
}
public class ArrayOperations {
public void reverse(Object[] array) {
// Reverses the order of elements in the array
}
public double sum(Object[] array) {
// Calculates the sum of elements in the array \
}
}
public class ArrayOperations {
public void reverse(T[] array) {
// Reverses the order of elements in the array
}
public double sum(T[] array) {
// Calculates the sum of elements in the array
}
}
public class ArrayOperations {
public void reverse(Object[] array) {
// Reverses the order of elements in the array
}
public double sum(Double[] array) {
// Calculates the sum of elements in the array
}
}
Question 2 MCQ · 6 marks
Consider the Java code given below.
The method boolean containsKey(Object key) in the class Map returns true if and only if the map contains an entry for a key k such that Objects.equals(key, k).
import java.util.*;
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Starting a Car");
}
}
class Motorcycle implements Vehicle {
public void start() {
System.out.println("Starting a Motorcycle");
}
}
class Garage<T extends Vehicle> {
private Map<String, T> vehicles;
public Garage() {
vehicles = new LinkedHashMap<String, T>(); //LINE A
}
public void add(String name, T vehicle) {
vehicles.put(name, vehicle);
}
public void startVehicle(String name) {
if (vehicles.containsKey(name)) {
T v = vehicles.get(name); //LINE B
v.start();
} else {
System.out.println("Vehicle not found");
}
}
}
public class TestGarage {
public static void main(String[] args) {
Garage<Vehicle> g = new Garage<Vehicle>();
Vehicle v1 = new Car();
Vehicle v2 = new Motorcycle();
g.add("car", v1);
g.add("motorcycle", v2);
g.startVehicle("car");
g.startVehicle("bicycle");
}
}
Choose the correct option.
LINE A generates compilation error because type T is not known
This program generates output:
Starting a Car
Vehicle not found
LINE B generates compilation error because a variable of type Vehicle cannot refer to objects of type Car and Motorcycle
This program generates output:
Starting a Car
Question 16 MSQ · 8 marks
Consider the Java code given below that prints the most impressive act among a set of given Performer objects. From among the options, identify the appropriate function header for the function printMostImpressiveAct that takes as input an array of Performer objects and prints the act with the highest score.
import java.util.*;
interface Performer {
int performAct();
}
class Magician implements Performer {
private int tricksPerformed;
public Magician(int tricks) {
this.tricksPerformed = tricks;
}
public int performAct() {
return tricksPerformed;
}
}
class Juggler implements Performer {
private int ballsJuggled;
public Juggler(int balls) {
this.ballsJuggled = balls;
}
public int performAct() {
return ballsJuggled;
}
}
public class TalentShow {
// LINE X: FUNCTION HEADER
{
// invokes method performAct()
// to print the value of the most impressive act
}
public static void main(String[] args) {
Performer[] performers = {new Magician(5), new Juggler(3), new Magician(8)};
printMostImpressiveAct(performers);
}
}
Choose the correct option(s).
public static void printMostImpressiveAct(<?> performers)
public static <T extends Performer> void printMostImpressiveAct(T[] performers)
public static <T extends Magician> void printMostImpressiveAct(T[] performers)
public static void printMostImpressiveAct(Performer[] performers)
2025 Mar 16
Question 16 MCQ · 8 marks
Consider the Java code given below.
public class MarksExample {
public <T extends Number> void calculateSum(T[] marks){
// Calculates the sum of marks
}
public <T> void displayMarks(T[] marks){
// Displays the marks
}
public <T extends Number> double calculateAverage(List<T> marks){
// Calculates the average of marks
}
}
How does class MarksExample look after type erasure?
public class MarksExample {
public void calculateSum(Object[] marks){
// Calculates the sum of marks
}
public void displayMarks(Object[] marks){
// Displays the marks
}
public Object calculateAverage(List<Object> marks){
// Calculates the average of marks
}
}
public class MarksExample {
public void calculateSum(Number[] marks){
// Calculates the sum of marks
}
public void displayMarks(Object[] marks){
// Displays the marks
}
public double calculateAverage(List<Number> marks){
// Calculates the average of marks
}
}
public class MarksExample {
public void calculateSum(Number[] marks){
// Calculates the sum of marks
}
public void displayMarks(Object[] marks){
// Displays the marks
}
public <T> T calculateAverage(List<Number> marks){
// Calculates the average of marks
}
}
public class MarksExample {
public <T> void calculateSum(T[] marks){
// Calculates the sum of marks
}
public <T> void displayMarks(T[] marks){
// Displays the marks
}
public <T> void calculateAverage(List<T> marks){
// Calculates the average of marks
}
}
Question 2 MSQ · 6 marks
Consider the Java code given below that prints the maximum element of a set. From among the options, identify the appropriate function header for function findMax that takes as input a set of numbers and prints the maximum element of the set.
import java.util.*;
class Test {
// FUNCTION HEADER for function findMax
{
// Prints the maximum element of the set
}
public static void main(String[] args) {
Set<Integer> s = new HashSet<>();
s.add(12);
s.add(23);
Set<Double> s1 = new HashSet<>();
s1.add(12.2);
s1.add(23.4);
findMax(s);
findMax(s1);
}
}
Choose the correct option(s).
public static void findMax(Set<T> s)
public static <T extends Number> void findMax(Set<T> s)
public static void findMax(Set<? extends Number> s)
public static void findMax(Set<Double> s)
Question 14 MSQ · 7 marks
Consider the Java code given below.
class RollNumber<T extends Number> {
private T part1;
private T part2;
public RollNumber(T p1, T p2) {
part1 = p1;
part2 = p2;
}
public T getPart1() {
return part1;
}
public T getPart2() {
return part2;
}
public RollNumber<Integer> genRollno(ARG) { //LINE 1
RollNumber<Integer> newroll = new RollNumber<>(0, 0);
newroll.part1 = this.part1.intValue() + r.getPart1().intValue();
newroll.part2 = this.part2.intValue() + r.getPart2().intValue();
return newroll;
}
}
public class Test {
public static void main(String[] args) {
RollNumber<Integer> r1 = new RollNumber<>(101, 200);
RollNumber<Integer> r2 = new RollNumber<>(102, 300);
RollNumber<Integer> newroll = r1.genRollno(r2);
System.out.println(newroll.getPart1() + "," + newroll.getPart2());
}
}
Choose all the options which can be used in place of ARG in LINE 1 so that the code outputs the sum of two roll numbers.
RollNumber<T> r
RollNumber<Number> r
RollNumber<?> r
RollNumber<Object> r
2026 Apr 06
Question 4 MCQ · 6 marks
Consider the Java code given below about controlling robot hardware values.
class RobotController {
public <T extends Number> void process(T value) { // LINE 1
System.out.println("Motor torque: " + (value.doubleValue() * 2));
}
public <U extends Number> void process(U value) { // LINE 2
System.out.println("Sensor gain: " + (value.doubleValue() * 4));
}
public <T> void process(T value) { // LINE 3
System.out.println("Command: " + value);
}
}
public class Test {
public static void main(String[] args) {
RobotController rc = new RobotController();
rc.process(5);
rc.process(2.1);
rc.process("MOVE_FORWARD");
}
}
Choose the correct option.
LINE 2 and LINE 3 generate a compilation error because method process() is already defined with the same signature.
LINE 1 and LINE 3 generate a compilation error because method process() is already defined with the same signature.
Only LINE 2 generates a compilation error because method process() is already defined with the same signature.
Motor torque: 10
Sensor gain: 8.4
MOVE_FORWARD
Question 2 MCQ · 6 marks
Consider the Java code given below that prints the credit points of courses. From among the options, identify the appropriate function header for the function printCredits that takes as input a list of courses and prints their credit points.
import java.util.*;
class Course {
private double credits;
public Course(double c) {
credits = c;
}
public double getCredits() {
return credits;
}
}
class MathCourse extends Course {
public MathCourse(double c) {
super(c);
}
}
class LinguisticsCourse extends Course {
public LinguisticsCourse(double c) {
super(c);
}
}
public class Test {
// FUNCTION HEADER for function printCredits
{
for (int i = 0; i < lst.size(); i++) {
System.out.println(lst.get(i).getCredits());
}
}
public static void main(String[] args) {
List<MathCourse> m = new ArrayList<MathCourse>();
m.add(new MathCourse(3.0));
m.add(new MathCourse(4.0));
List<LinguisticsCourse> l = new ArrayList<LinguisticsCourse>();
l.add(new LinguisticsCourse(2.5));
l.add(new LinguisticsCourse(3.5));
printCredits(m);
printCredits(l);
}
}
Choose the correct option.
public static void printCredits(List<Course> lst)
public static void printCredits(List<LinguisticsCourse> lst)
public static void printCredits(List<? extends Course> lst)
public static void printCredits(List<Object> lst)
2025 Aug 03
Question 2 MCQ · 6 marks
Consider the Java code given below.
class Printer {
public <T extends Number> void printDetails(T value) { // LINE 1
System.out.println("Number: " + (value.doubleValue() * 3));
}
public <U extends Number> void printDetails(U value) { // LINE 2
System.out.println("Double Number: " + (value.doubleValue() * 6));
}
public <T> void printDetails(T value) { // LINE 3
System.out.println("Value: " + value);
}
}
public class Test {
public static void main(String[] args) {
Printer p = new Printer();
p.printDetails(7);
p.printDetails(3.5);
p.printDetails("Java");
}
}
Choose the correct option.
LINE 2 and LINE 3 generates a compilation error because method printDetails() is already defined with the same signature.
LINE 1 and LINE 3 generates a compilation error because method printDetails() is already defined with the same signature.
Only LINE 2 generates a compilation error because method printDetails() is already defined with the same signature.
The program compiles and produces the output:
Number: 21.0
Double Number: 21.0
Value: Java
Question 12 MCQ · 8 marks
Consider the Java code given below.
public class GenericExample {
public <T extends Comparable<T>> boolean compareValues(T a, T b) {
// check whether a equal b
}
public <T> void printValues(T[] values) {
// display values
}
public <T extends Number> double sumValues(List<T> values) {
// add the values
}
}
How does class GenericExample look after type erasure?
public class GenericExample {
public boolean compareValues(Object a, Object b) {
// check whether a equal b
}
public void printValues(Object[] values) {
// display values
}
public double sumValues(List<Object> values) {
// add the values
}
}
public class GenericExample {
public boolean compareValues(Comparable a, Comparable b) {
// check whether a equal b
}
public void printValues(Object[] values) {
// display values
}
public double sumValues(List<Object> values) {
// add the values
}
}
public class GenericExample {
public boolean compareValues(T a, T b) {
// check whether a equal b
}
public void printValues(T[] values) {
// display values
}
public double sumValues(List<T> values) {
// add the values
}
}
public class GenericExample {
public boolean compareValues(Comparable a, Comparable b) {
// check whether a equal b
}
public void printValues(Object[] values) {
// display values
}
public double sumValues(List<Number> values) {
// add the values
}
}
Question 16 MSQ · 8 marks
Consider the Java code given below, which prints the names of employees. From among the options, identify the appropriate function header(s) for the function printEmployeeNames that takes as input a list of employees and prints their names.
import java.util.*;
class Employee {
private String name;
public Employee(String n) {
name = n;
}
public String getName() {
return name;
}
}
class Manager extends Employee {
public Manager(String n) {
super(n);
}
}
class Developer extends Employee {
public Developer(String n) {
super(n);
}
}
public class Company {
// FUNCTION HEADER for function printEmployeeNames
{
for (int i = 0; i < empList.size(); i++) {
System.out.print(empList.get(i).getName() + ", ");
}
}
public static void main(String[] args) {
List<Manager> managers = new ArrayList<>();
managers.add(new Manager("Rohan"));
managers.add(new Manager("Arjun"));
List<Developer> developers = new ArrayList<>();
developers.add(new Developer("Karthik"));
developers.add(new Developer("Diya"));
printEmployeeNames(managers);
printEmployeeNames(developers);
}
}
Choose the correct option(s).
public static void printEmployeeNames(List<? extends Employee> empList)
public static <T extends Employee> void printEmployeeNames(List<T> empList)
public static void printEmployeeNames(List<Object> empList)
public static void printEmployeeNames(List<Developer> empList)
2022 July
Question 4 MCQ · 3 marks
Consider the following code.
public class ArrayOperations{
public <T extends Comparable> T min(T[] arr){
// code for finding the minimum
}
public <T extends Number> T sum(T[] arr){
// code for finding the sum of elements
}
public <T> int count(T[] arr){
// code for finding the count of elements
}
}
What is the class ArrayOperations converted to after type erasure?
public class ArrayOperations{
public Object min(Object[] arr){
// code for finding the minimum
}
public Number sum(Number[] arr){
// code for finding the sum of elements
}
public int count(Object[] arr){
// code for finding the count of elements
}
}
public class ArrayOperations{
public Object min(Object[] arr){
// code for finding the minimum
}
public Object sum(Object[] arr){
// code for finding the sum of elements
}
public int count(Object[] arr){
// code for finding the count of elements
}
}
public class ArrayOperations{
public Comparable min(Comparable[] arr){
// code for finding the minimum
}
public Number sum(Number[] arr){
// code for finding the sum of elements
}
public int count(Object[] arr){
// code for finding the count of elements
}
}
public class ArrayOperations{
public Comparable min(Comparable[] arr){
// code for finding the minimum
}
public Integer sum(Integer[] arr){
// code for finding the sum of elements
}
public int count(Object[] arr){
// code for finding the count of elements
}
}
Question 14 MSQ · 4 marks
The code given below should print true if the sum of elements of the array is positive, else false. Identify the header(s) for function checkSum (at LINE 1) such that the code prints either true or false, based on value of the sum.
interface Summable{
public double sum();
}
class ArrayOperations<T extends Number> implements Summable{
// instance variables and constructors here
private T[] arr;
public ArrayOperations(T[] y){
arr = y;
}
public double sum(){ //Finds the sum of elements in arr
double sum = 0;
for(T x : arr){
sum = sum + x.doubleValue();
}
return sum;
}
}
public class Test {
//LINE 1
{
if(ob.sum() > 0){
return true;
}
return false;
}
public static void main(String[] args){
Double[] r = {12.3, 14.6, 34.5, 67.0};
ArrayOperations<Double> d = new ArrayOperations<Double>(r);
System.out.println(checkSum(d));
}
}
public static <T extends Summable> boolean checkSum(T ob)
public static boolean checkSum(Summable ob)
public static boolean checkSum(T ob)
public static <T> boolean checkSum(T ob)
Question 15 MSQ · 4 marks
Consider the following program.
class ComplexNumber<T extends Number>{
private T a;
private T b;
public ComplexNumber(T a_val, T b_val){
a = a_val;
b = b_val;
}
public T get_a(){
return a;
}
public T get_b(){
return b;
}
public ComplexNumber<Double> subtract(____LINE 1____){
ComplexNumber<Double> c1 = new ComplexNumber<>(0.0, 0.0);
c1.a = this.a.doubleValue() - c.get_a().doubleValue();
c1.b = this.b.doubleValue() - c.get_b().doubleValue();
return c1;
}
}
public class Test{
public static void main(String args[]){
ComplexNumber<Integer> c1 = new ComplexNumber<Integer>(5, 6);
ComplexNumber<Integer> c2 = new ComplexNumber<Integer>(3, 2);
ComplexNumber<Double> c3 = c1.subtract(c2);
System.out.println(c3.get_a() + "," + c3.get_b());
}
}
Choose all the options which can be used in place of LINE 1 so that the code outputs the difference between two complex numbers.
ComplexNumber<T> c
ComplexNumber<Number> c
ComplexNumber<?> c
ComplexNumber<Object> c
Check your answers →
Weeks covering this topic
Other Java topics