Question 1
Match the following terms with their descriptions.
A-4, B-1, C-2, D-3
A-1, B-4, C-3, D-2
A-4, B-2, C-1, D-3
A-2, B-1, C-4, D-3
The IIT Madras BS Programming Concepts using Java (Java) Quiz 1 paper sat on 26 Feb 2023, in the January 2023 term: 15 questions for 50 marks in 120 minutes. Every question is below with its answer. Take it as a timed mock test to be marked, or read it through first.
Match the following terms with their descriptions.
A-4, B-1, C-2, D-3
A-1, B-4, C-3, D-2
A-4, B-2, C-1, D-3
A-2, B-1, C-4, D-3
Correct answer
A-4, B-1, C-2, D-3
Consider the code given below.
class Institute{ public void teach() { System.out.println("Teaches"); }}class College extends Institute{ public void sports() { System.out.println("College sports"); } public void research() { System.out.println("College research"); }}class University extends College{ public void research() { System.out.println("University research"); }}public class Test{ public static void main(String[] args) { College obj = new University(); obj.teach(); //LINE 1 obj.sports(); obj.research(); //LINE 2 }}Choose the correct option.
Correct answer
Consider the code given below.
class Employee{ public final void bonus(){ System.out.println("Employee bonus"); }}class TeamManager extends Employee{ public void bonus(){ System.out.println("Team manager bonus"); }}class Manager extends TeamManager{}public class Example{ public static void main(String[] args){ Manager m = new Manager(); m.bonus(); }}Choose the correct option regarding the given code.
Correct answer
Consider the code given below.
class Professor{ public void printInfo(){ System.out.println("Professor info"); }}class HOD extends Professor{ public void printInfo(){ System.out.println("HOD info"); } public void additionalDuties(){ System.out.println("additional duties"); }}public class Test{ public static void main(String[] args){ Professor obj = new HOD(); obj.printInfo(); obj.additionalDuties(); }}Choose the correct option regarding the given code.
Correct answer
Consider the code given below.
class Date{ int date, month, year; //Constructor to initialize instance variables public String toString() { return "DOB = " + date + "-" + month + "-" + year; }}class Student{ private String name; private Date dob; public Student(String name) { this.name = name; } public Student(Student s) { this.name = s.name; } public void setDob(Date dob) { this.dob = dob; } public String toString() { return "name = " + name+", "+dob; }}public class ConTest { public static void main(String[] args) { Student obj1 = new Student("ABC"); obj1.setDob(new Date(1, 6, 1990)); Student obj2 = new Student(obj1); obj2.setDob(new Date(31, 1, 1992)); System.out.println(obj1); System.out.println(obj2); }}What will the output be?
Correct answer
Consider the code given below.
public class SwitchTest { public static void main(String[] args) { int arr[] = {1, 2, 3}; int count = 0; for (int i : arr) { switch (i) { case 2: count += 1; case 1: count += 2; default: count += 3; } } System.out.println(count); }}What will the output be?
0
3
14
6
Correct answer
14
Consider the code given below.
class Sample{ private int a; private long b; private double c; public Sample(int x, long y) { a = x; b = y; } public Sample(long p, double q) { b = p; c = q; } public void getResult() { String result = a+b+c+""; // LINE 1 System.out.println(result); }}public class VarTest { public static void main(String[] args) { Sample s1 = new Sample(10, 20); Sample s2 = new Sample(40, 50.0); s1.getResult(); s2.getResult(); }}Choose the correct option.
Correct answer
Consider the code given below.
interface Promotable{ boolean isPromoted(); default void promoted() { System.out.println("Promoted to final year"); } default void detained() { System.out.println("Not promoted to final year"); }}class UGStudent implements Promotable{ String name; int credits; public UGStudent(String n, int c) { //initialized instance variables University u = new University(this); u.promote(); } public boolean isPromoted() { if(credits >= 73) return true; return false; }}public class University { private Promotable obj; //Constructor to initialize instance variables public void promote() { if(obj.isPromoted()) obj.promoted(); else obj.detained(); } public static void main(String[] args) { Promotable s1 = new UGStudent("ABC", 74); Promotable s2 = new UGStudent("XYZ", 66); }}Choose the correct option.
Correct answer
Consider the code given below.
abstract class OnlineShop{ abstract void delivery(); //LINE-1 public void returns() { //LINE-2 System.out.println("Returns accepted"); }}class Keshoo extends OnlineShop{ public void delivery() { System.out.println("Keesho delivers products"); }}class Kyntra extends OnlineShop{ public void delivery() { System.out.println("Kyntra delivers products"); }}public class AbstractEx { public static void main(String[] args) { OnlineShop[] os = new OnlineShop[2]; os[0] = new Keshoo(); //LINE-3 os[1] = new Kyntra(); //LINE-4 for (int i = 0; i < os.length; i++) { os[i].delivery(); os[i].returns(); } }}Choose the correct option.
Correct answer
Consider the code given below.
interface Vehicle{ default void getNoOfWheels() { System.out.println("Has 2 Wheels"); } default void getFuelCapacity() { System.out.println("Capacity: 20L Petrol"); }}class ADMSBike implements Vehicle{ // LINE 1 public void getFuelCapacity() { System.out.println("No petrol needed"); }}class MaruthiCar implements Vehicle{ // LINE 2 public void getNoOfWheels() { System.out.println("Has 4 Wheels"); }}public class InterfaceTest { public static void main(String[] args) { Vehicle v[] = new Vehicle[2]; v[0] = new ADMSBike(); v[1] = new MaruthiCar(); for (int i = 0; i < v.length; i++) { v[i].getNoOfWheels(); v[i].getFuelCapacity(); } }}Choose the correct option.
Correct answer
Consider the code given below.
interface Workable{ void work();}class Human{ public Workable getHeart() { return new Heart(); } public Workable getKidney() { return new Kidney(); } private class Heart implements Workable{ public void work() { System.out.println("Pumps blood"); } } private class Kidney implements Workable{ public void work() { System.out.println("Removes wastes"); } }}public class PrivateTest { public static void main(String[] args) { //CODE BLOCK }}Choose the correct option to be filled in place of CODE BLOCK so that the output is:
Pumps blood
Removes wastes
Correct answer
Which of the following statements is/are correct?
Return value link points to the start of the previous activation record.
An activation record gets pushed into the stack when a function is called, and popped out when the function returns.
The variables present in every activation record in the stack are in scope and are accessible.
The variables present in the topmost activation record of the stack are in scope and are accessible.
Correct answers
An activation record gets pushed into the stack when a function is called, and popped out when the function returns.
The variables present in the topmost activation record of the stack are in scope and are accessible.
Consider the code given below that checks whether two vehicles are the same. Method equals is overridden to compare two Vehicle objects as follows. If two vehicles have the same registration number, then they are the same. Based on the given information, answer the question that follows.
class Vehicle{ private String regno;
//Constructor to initialize instance variables
public String toString() { return regno; } public boolean equals(Object obj) { // CODE BLOCK }}public class Test { public static void main(String[] args) { Vehicle v1 = new Vehicle("RC12345"); Vehicle v2 = new Vehicle("RC99999"); Vehicle v3 = new Vehicle("RC99999"); if(v1.equals(v3)) System.out.println(v1+", "+v3+" are same"); if(v2.equals(v3)) System.out.println(v2+", "+v3+" are same"); }}Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:
RC99999, RC99999 are same
Correct answer
Consider the code given below.
class Calculator{ public int add(int a, int b){ return a+b; }}class SmartCalculator extends Calculator{ public int add(int a, int b, int c){ return a + b + c; } public void divide(){ System.out.println("Prints quotient and remainder"); }}public class User{ public static void main(String[] args){ Calculator c = new Calculator(); SmartCalculator sc = new SmartCalculator(); // LINE 1 }}Choose the correct option(s) that can be filled in place of LINE 1 such that it does not generate any compile time error.
Correct answers
Consider the code given below.
class Developer{ private String name; private String dept;
//CODE BLOCK
public void getDetails() { System.out.println(name+" "+dept); }}public class Test { public static void main(String[] args) { Developer obj = new Developer("XYZ", "Java"); obj.getDetails(); }}Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:
XYZ Java
Correct answers