23 questions · 111 marks · 8 papers. Try each one before checking.
Solve them and check answers →Read the notes first
2023 Aug
Question 2 MCQ · 3 marks
Consider the Java code given below.
interface Computable{
void compute();
}
class Phone implements Computable{
public void compute(){
System.out.println("Phone compute");
}
}
class Laptop implements Computable{
public void compute(){
System.out.println("Laptop compute");
}
}
class DeviceList{
private Object[] cArr = {new Phone(), new Laptop()};
public void getCompute(){
for(int i = 0; i < cArr.length; i++){
//LINE 1
}
}
}
public class Test{
public static void main(String[] args) {
DeviceList dList = new DeviceList();
dList.getCompute();
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
Phone compute
Laptop compute
cArr[i].compute();
((Computable)cArr[i]).compute();
((Phone)cArr[i]).compute();
((Laptop)cArr[i]).compute();
2023 Dec
Question 3 MCQ · 3 marks
Consider the Java code given below.
interface Language{
void display();
}
class French implements Language{
public void display() {
System.out.println("French Language");
}
}
class English implements Language{
public void display() {
System.out.println("English Language");
}
}
class LanguageList{
private Object[] LArr = {new French(), new English()};
public void getInfo(){
for(int i = 0; i < LArr.length; i++) {
//LINE 1
}
}
}
public class Test{
public static void main(String[] args) {
LanguageList aList = new LanguageList();
aList.getInfo();
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
French Language
English Language
LArr[i].display();
((English)LArr[i]).display();
((French)LArr[i]).display();
((Language)LArr[i]).display();
2024 Mar 24
Question 10 MCQ · 7 marks
Consider the Java code given below.
interface Chargeable {
void charge();
}
class SmartPhone implements Chargeable {
public void charge() {
System.out.println("Charging SmartPhone");
}
}
class SmartWatch implements Chargeable {
public void charge() {
System.out.println("Charging SmartWatch");
}
}
class DeviceList {
private Object[] cArr = {new SmartPhone(), new SmartWatch()};
public void chargeDevices() {
for (int i = 0; i < cArr.length; i++) {
// LINE 1
}
}
}
public class Test {
public static void main(String[] args) {
DeviceList dList = new DeviceList();
dList.chargeDevices();
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
Charging SmartPhone
Charging SmartWatch
cArr[i].charge();
((Chargeable)cArr[i]).charge();
((SmartPhone)cArr[i]).charge();
((SmartWatch)cArr[i]).charge();
Question 14 MSQ · 8 marks
Consider the Java code given below.
public interface Animal{
public abstract void makeSound();
}
public class Zoo{
private String category;
public void setCategory(String s) {
this.category = s;
}
public String getCategory() {
return category;
}
public Animal createAnimal() {
if (getCategory() == "Mammal") {
return new Mammal();
}
return new Bird();
}
private class Mammal implements Animal{
public void makeSound() {
System.out.println("Mammal making a sound");
}
}
private class Bird implements Animal {
public void makeSound() {
System.out.println("Bird chirping");
}
}
}
public class Example {
public static void main(String[] args) {
Zoo z = new Zoo();
z.setCategory("Mammal");
// --------- Line 1 ---------
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
Mammal making a sound
z.makeSound();
z.createAnimal().makeSound();
((Animal) z.createAnimal()).makeSound();
Animal a = z.createAnimal();
a.makeSound();
2024 Aug 04
Question 9 MCQ · 7 marks
Consider the Java code given below.
interface Operable {
void operate();
}
class Robot implements Operable {
public void operate() {
System.out.println("Operating Robot");
}
}
class Drone implements Operable {
public void operate() {
System.out.println("Operating Drone");
}
}
class MachineShop {
private Object[] oArr = new Object[]{new Robot(), new Drone(), "String Object"};
public void operateMachines() {
for (int i = 0; i < oArr.length; i++) {
// LINE X
}
}
}
public class TestMachines {
public static void main(String[] args) {
MachineShop mShop = new MachineShop();
mShop.operateMachines();
}
}
Identify the appropriate option to fill in place of LINE X such that the output is:
Operating Robot
Operating Drone
oArr[i].operate();
if(oArr[i] instanceof Operable)
((Operable)oArr[i]).operate();
((Robot)oArr[i]).operate();
((Drone)oArr[i]).operate();
Question 15 MSQ · 8 marks
Consider the Java code given below.
interface Element {
void display();
}
class PeriodicTableElement {
private String group;
public void setGroup(String g) {
this.group = g;
}
public String getGroup() {
return group;
}
public Element createElement() {
switch (getGroup()) {
case "Metal":
return new Metal();
case "Nonmetal":
return new Nonmetal();
case "Metalloid":
return new Metalloid();
default:
return null;
}
}
private class Metal implements Element {
public void display() {
System.out.println("Metal properties");
}
}
private class Nonmetal implements Element {
public void display() {
System.out.println("Nonmetal properties");
}
}
private class Metalloid implements Element {
public void display() {
System.out.println("Metalloid behavior");
}
}
}
public class ChemistryLab {
public static void main(String[] args) {
PeriodicTableElement pt = new PeriodicTableElement();
pt.setGroup("Metalloid");
// --------- Line X ---------
}
}
Identify the appropriate option to fill in place of LINE X such that the output is:
Metalloid behavior
Element e = pt.createElement();
e.display();
pt.display();
pt.createElement().display();
Element e = pt;
e.display();
2025 Mar 16
Question 11 MCQ · 7 marks
Consider the Java code given below.
interface Washable {
void showWashable();
}
class FrontLoadWasher implements Washable{
public void showWashable() {
System.out.println("FLW is washable");
}
}
class TopLoadWasher implements Washable {
public void showWashable() {
System.out.println("TLW is washable");
}
}
class ApplianceList {
private Object[] appliances = {new FrontLoadWasher(), new TopLoadWasher()};
public void testAppliances() {
for (int i = 0; i < appliances.length; i++) {
//LINE-1
}
}
}
public class Test {
public static void main(String[] args) {
ApplianceList aL = new ApplianceList();
aL.testAppliances();
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
FLW is washable
TLW is washable
((Washable) appliances[i]).showWashable();
(appliances[i]).showWashable();
((FrontLoadWasher) appliances[i]).showWashable();
((TopLoadWasher) appliances[i]).showWashable();
Question 3 MCQ · 6 marks
Consider the Java code given below.
interface Deliverable {
public static void status() {
System.out.println("Delivery status");
}
public default void deliver() {
System.out.println("Order delivered");
}
}
interface Trackable {
public static void status() {
System.out.println("Tracking status");
}
public default void track() {
System.out.println("Tracking order");
}
}
class FoodOrder implements Deliverable, Trackable {
public void deliver() {
System.out.println("Delivered successfully");
}
}
public class Test {
public static void main(String[] args) {
Deliverable o1 = new FoodOrder();
o1.deliver();
o1.track();
Deliverable.status(); //LINE-1
Trackable.status(); //LINE-2
}
}
Choose the correct option.
This program generates the output:
Delivered successfully
Tracking order
Delivery status
Tracking status
This program generates the output:
Delivered successfully
Tracking order
Tracking status
Tracking status
This program generates compiler error because o1 of type Deliverable cannot invoke method track().
LINE-1 and LINE-2 generates a compilation error as any methods cannot be accessed using the interface name.
2026 Apr 06
Question 5 MCQ · 6 marks
Consider the Java code given below.
interface Service {
void process();
}
class CheckIn implements Service {
public void process() {
System.out.println("Check-In Service");
}
}
class Boarding implements Service {
public void process() {
System.out.println("Boarding Service");
}
}
class ServiceList {
private Object[] SArr = { new CheckIn(), new Boarding() };
public void run() {
for (int i = 0; i < SArr.length; i++) {
// LINE 1
}
}
}
public class Airport {
public static void main(String[] args) {
ServiceList SL = new ServiceList();
SL.run();
}
}
Identify the appropriate option to fill in place of LINE 1 such that the output is:
Check-In Service
Boarding Service
SArr[i].process();
((Boarding)SArr[i]).process();
((CheckIn)SArr[i]).process();
((Service)SArr[i]).process();
Question 6 MCQ · 6 marks
Consider the code given below.
class Operation {
public void apply() {
System.out.println("General linear operation");
}
}
class MatrixOp extends Operation {
public void compute() {
System.out.println("Matrix computation");
}
public void describe() {
System.out.println("Matrix description");
}
}
class SparseMatrixOp extends MatrixOp {
public void describe() {
System.out.println("Sparse Matrix description");
}
}
public class Test {
public static void main(String[] args) {
MatrixOp ob = new SparseMatrixOp();
ob.apply(); // LINE 1
ob.compute();
ob.describe(); // LINE 2
}
}
Choose the correct option.
LINE 1 generates compilation error because method apply() cannot be invoked on ob.
This code generates the output followed by runtime Error at LINE 2 due to ambiguity in describe().
General linear operation
Matrix computation
Sparse Matrix description
General linear operation
Matrix computation
Matrix description
Question 7 MCQ · 7 marks
Consider the Java code given below.
class CelestialBody {
private String name;
public CelestialBody(String n) {
name = n;
}
public String toString() {
return "Name: " + name;
}
}
class Planet extends CelestialBody {
private String type;
public Planet(String n, String t) {
//LINE-1
type = t;
}
public String toString() {
return super.toString() + ", Type: " + type;
}
}
public class Test {
public static void main(String[] args) {
CelestialBody exo = new Planet("Kepler-452b", "Super-Earth");
System.out.println(exo);
}
}
Choose the correct option to be filled in place of LINE-1 so that the output is:
Name: Kepler-452b, Type: Super-Earth
this(n);
super(n);
CelestialBody(n);
super.CelestialBody(n);
2026 Jul 16
Question 15 MSQ · 3 marks
Consider the Java program below.
class Matrix {
public void displayType() {
System.out.println("General Matrix");
}
}
class DiagonalMatrix extends Matrix {
public void displayType() {
System.out.println("Diagonal Matrix");
}
public void determinant() {
System.out.println("Determinant is product of diagonal elements");
}
}
public class TestMatrix {
public static void main(String[] args) {
Matrix m = new DiagonalMatrix();
m.displayType();
______________________// LINE 1
}
}
Identify the correct option(s) to fill in the blank at LINE 1, such that the output is:
Diagonal Matrix
Determinant is product of diagonal elements
m.determinant();
DiagonalMatrix.determinant();
if(m instanceof DiagonalMatrix) {
DiagonalMatrix dm = (DiagonalMatrix)m;
dm.determinant();
}
((DiagonalMatrix)m).determinant();
Question 4 MCQ · 3 marks
Consider the Java code given below.
interface SwitchOn {
default void on() {
System.out.println("Device switched on");
}
}
interface SwitchOff {
default void off() {
System.out.println("Device switched off");
}
}
class SmartLight implements SwitchOn, SwitchOff { //LINE 1
public void on() {
System.out.println("Smart light switched on");
}
}
public class Test {
public static void main(String[] args) {
SwitchOn s1 = new SmartLight();
s1.on();
s1.off(); //LINE 2
}
}
Choose the correct option.
LINE 1 generates compilation error because the class SmartLight is not declared as abstract.
LINE 2 generates compilation error because s1 of type SwitchOn cannot invoke method off().
Smart light switched on
Device switched off
Device switched on
Device switched off
Question 7 MCQ · 3 marks
Consider the Java program below.
abstract class Sensor {
public abstract void calibrate();
public void showData(){
System.out.println("Sensor shows data");
}
}
class TemperatureSensor extends Sensor {
public void showData(){
super.showData();
System.out.println("Temperature sensor shows data");
}
}
public class Lab {
public static void main(String[] args) {
Sensor s = new TemperatureSensor();
s.showData();
}
}
Choose the correct option regarding the output of the code.
Sensor shows data
Temperature sensor shows data
Sensor shows data
Temperature sensor shows data
This code generates a compile time error.
Question 8 MCQ · 3 marks
Consider the following Java code.
interface Device {
public default void start() {
System.out.println("cannot start device");
}
public default void stop() {
System.out.println("cannot stop device");
}
}
abstract class Machine {
public abstract void process();
public void start(){
System.out.println("Machine starts");
}
}
class Robot extends Machine implements Device {
public void process(){
System.out.println("Robot processing");
}
}
public class FClass {
public static void main(String[] args) {
Robot r = new Robot();
r.start();
r.process();
r.stop();
}
}
Identify the correct option for the code given above.
Machine starts
Robot processing
cannot stop device
cannot start device
Robot processing
cannot stop device
This code generates a compile time error, since the implementations of start() from Device and Machine are in conflict with each other.
This code generates a compile time error since stop() is not implemented in class Robot
Question 10 MCQ · 3 marks
Consider the Java code given below.
interface RideCallback {
void onRideArrived(String cab);
}
class Passenger implements RideCallback {
public void bookRide(String cab) {
Driver d = new Driver(this); // LINE 1
d.startRide(cab);
}
public void onRideArrived(String cab) {
System.out.println("Passenger: " + cab + " arrived!");
}
}
class Driver {
private RideCallback callback;
public Driver(RideCallback cb) {
callback = cb;
}
public void startRide(String cab) {
System.out.println("Driver: Driving " + cab + "...");
System.out.println("Driver: " + cab + " has arrived.");
callback.onRideArrived(cab); // LINE 2
}
}
public class CabService {
public static void main(String[] args) {
Passenger p1 = new Passenger();
p1.bookRide("Taxi");
}
}
Choose the correct option.
This program generates compiler error at LINE 1 Reason: An object of Driver cannot be created inside method Passenger because it uses an object of RideCallback in its constructor.
Driver: Driving Taxi...
Driver: Taxi has arrived.
This program generates compiler error at LINE 2 Reason: A method inside class Passenger is invoking method startRide inside class Driver, whereas Driver is invoking another method in class Passenger.
Driver: Driving Taxi...
Driver: Taxi has arrived.
Passenger: Taxi arrived!
Question 13 MCQ · 4 marks
Consider the code given below.
interface BookIterator {
public boolean hasNextBook();
public Object getNextBook();
}
abstract class Book {
public abstract void read();
}
class Library {
private final int count = 3;
private Novel[] books = {
new Novel("Book A"), new Novel("Book B"),
new Novel("Book C")};
private class Novel extends Book {
private String title;
public Novel(String t) {
this.title = t;
}
public void read() {
System.out.println("Reading: " + title);
}
}
private class LibraryIter implements BookIterator {
private int currentIndex;
public LibraryIter() {
// Constructor
}
public boolean hasNextBook() {
// Check if there is a next book
}
public Object getNextBook() {
// Return the next book
}
}
public BookIterator getIterator() {
return new LibraryIter();
}
}
Identify the appropriate statement to fill in the blank at LINE 1, such that the output is:
public class LibraryTest {
public static void main(String[] args) {
Library library = new Library();
BookIterator iter = library.getIterator();
while (iter.hasNextBook()){
_______________________; //LINE 1
}
}
}
Output:
Reading: Book A
Reading: Book B
Reading: Book C
((Book)iter.getNextBook()).read();
((Novel)iter.getNextBook()).read();
((Library)iter.getNextBook()).read();
iter.getNextBook().read();
Question 2 MCQ · 3 marks
Consider the Java code given below.
class Laptop {
private String brand;
private int ram;
public Laptop(String b, int r) {
brand = b;
ram = r;
}
***-------------***
* CODE SEGMENT *
***-------------***
}
public class Store {
public static void main(String[] args) {
Laptop l1 = new Laptop("Dell", 16);
System.out.println(l1.toString());
System.out.println(l1);
}
}
Choose the correct option to fill in the CODE SEGMENT so that the output is:
Dell : 16GB
Dell : 16GB
public String toString(){
return brand + " : " + ram + "GB";
}
public void toString(){
System.out.println(brand + " : " + ram + "GB");
}
public String toString(Object ob){
return ob.brand + " : " + ob.ram + "GB";
}
String toString(){
return super.toString();
}
Question 3 MCQ · 3 marks
Consider the Java code given below.
class SortingAlgorithm {
private int size;
private static int complexityFactor = 2;
public SortingAlgorithm(int n) {
size = n;
}
public final int timeComplexity() {
return complexityFactor * size;
}
}
class MergeSort extends SortingAlgorithm {
public MergeSort(int n) {
super(n);
}
public final int timeComplexity() { // LINE 1
return complexityFactor * 100; // LINE 2
}
}
public class AlgorithmTest {
public static void main(String[] args) {
MergeSort m1 = new SortingAlgorithm(100); // LINE 3
SortingAlgorithm s1 = new MergeSort(200); // LINE 4
System.out.println(s1.timeComplexity());
System.out.println(m1.timeComplexity());
}
}
Which of the following statements is FALSE?
LINE 1 generates compilation error because the method timeComplexity() cannot be overridden.
LINE 2 generates compilation error because variable complexityFactor cannot be accessed in class MergeSort.
LINE 3 generates compilation error because a variable of type MergeSort cannot refer to an object of type SortingAlgorithm.
LINE 4 generates compilation error because a variable of type SortingAlgorithm cannot refer to an object of type MergeSort.
Question 11 MCQ · 4 marks
Consider the code given below.
class DataProcessor {
public void processData() {
System.out.println("Processing general research data");
}
public void processData(String fileName) {
System.out.println("Processing file: " + fileName);
}
}
class GenomeDataProcessor extends DataProcessor {
public void processData(String genomeFile) {
System.out.println("Analyzing genome sequence file: "
+ genomeFile);
}
}
public class ResearchLab {
public static void main(String[] args) {
DataProcessor processor =
new GenomeDataProcessor(); // LINE 1
processor.processData();
processor.processData("covid_sequence.txt"); // LINE 2
}
}
Choose the correct option.
LINE 1 generates compilation error because a variable of type DataProcessor cannot refer to an object of type GenomeDataProcessor.
Processing general research data
Processing general research data
Processing file: covid_sequence.txt
Processing general research data
Analyzing genome sequence file: covid_sequence.txt
Question 12 MCQ · 4 marks
Consider the code given below that checks whether two software modules are the same. Method equals is overridden to compare two Module objects as follows. If two modules have the same moduleName and moduleId, then they are considered the same.
class Module {
private String moduleName;
private int moduleId;
// Constructor to initialize instance variables
public boolean equals(Object obj) {
//CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Module m1 = new Module("Authentication", 101);
Module m2 = new Module("Authentication", 101);
if (m1.equals(m2))
System.out.println("m1 and m2 are same");
else
System.out.println("m1 and m2 are different");
}
}
Choose the correct option to fill in place of CODE BLOCK so that the output is:
m1 and m2 are same
if (obj instanceof Module) {
if (this.moduleName == obj.moduleName &&
this.moduleId == obj.moduleId)
return true;
}
return false;
if (this.moduleName == obj.moduleName &&
this.moduleId == obj.moduleId)
return true;
return false;
if (obj instanceof Module) {
Module m = obj;
if (this.moduleName == m.moduleName &&
this.moduleId == m.moduleId)
return true;
}
return false;
if (obj instanceof Module) {
Module m = (Module) obj;
if (this.moduleName == m.moduleName &&
this.moduleId == m.moduleId)
return true;
}
return false;
Question 14 MCQ · 4 marks
Consider the Java code given below.
public class Employee{
private int empid;
private String name;
public Employee(int empid_, String name_) {
empid = empid_;
name = name_;
}
public Employee() {
this(0, "unknown");
}
public void print(){
System.out.print(empid + " : " + name + " : ");
}
}
public class Manager extends Employee{
private String department;
public Manager(int empid_, String name_, String department_) {
//line:1
department = department_;
}
public void print(){
super.print();
System.out.println(department);
}
}
public class FClass{
public static void main(String[] args){
Manager m = new Manager(101, "Nutan", "HR");
m.print();
}
}
Identify the appropriate option to fill in the blank at line:1 such that output of the code will be:
101 : Nutan : HR
Employee(empid_, name_ );
this(empid_, name_);
super(empid_, name_);
empid = empid_; name = name_;
2025 Aug 03
Question 8 MCQ · 6 marks
Consider the code given below.
class Dancer {
public void perform() {
System.out.println("Dancer performs");
}
public void perform(String style) {
System.out.println("Dancer performs " + style);
}
}
class StreetDancer extends Dancer {
public void perform(String move) {
System.out.println("StreetDancer performs " + move);
}
}
public class TestDance {
public static void main(String[] args) {
Dancer d = new StreetDancer(); // LINE 1
d.perform();
d.perform("hip-hop"); // LINE 2
}
}
Choose the correct option.
LINE 1 generates a compilation error because a reference of type Dancer cannot point to an object of type StreetDancer.
This code generates the below output followed by runtime Error at LINE 2 because there is ambiguity in which perform() method is being invoked.
Dancer performs
This code generates the output:
Dancer performs
Dancer performs hip-hop
This code generates the output:
Dancer performs
StreetDancer performs hip-hop
Check your answers →
Weeks covering this topic
Other Java topics