Quiz Space

May 2023 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 1: 16 July 2023 (May 2023 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 1 paper sat on 16 Jul 2023, in the May 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.

Questions
15
Marks
50
Duration
120 min
MSQ
4
MCQ
11

Updated

Official paper: IIT M FOUNDATION AN2 EXAM QPF2 16 JULY 2023 · No negative marking.

Question 1

+4 marksOne or more correct options

Which among the following statements is/are true about activation records?

Select all that apply.

  1. A

    All the variables on the stack (in any activation record) are in their scope.

  2. B

    Only the variables present in the topmost activation record of the stack are in their lifetime

  3. C

    Every activation record has a control link that points to start of previous record.

  4. D

    Every activation record has a return value link that points to where to store return value.

Show answer

Correct answers

  • C

    Every activation record has a control link that points to start of previous record.

  • D

    Every activation record has a return value link that points to where to store return value.

Question 2

+4 marksOne or more correct options

Consider the code given below that checks whether two books are the same. Method equals is overridden to compare two Book objects as follows. If two books have the same author name and book name, then they are the same. Based on the given information, answer the question that follows.

java
class Book{
private String bName; // Book name
private String aName; // Author name
//Constructor to initialize instance variables
public String toString() {
return bName;
}
public boolean equals(Object obj) {
// CODE BLOCK
}
}
public class Test {
public static void main(String[] args) {
Book b1 = new Book("ABC", "XYZ");
Book b2 = new Book("ABC", "XYZ");
if(b1.equals(b2))
System.out.println(b1+", "+ b2 +" are same");
else
System.out.println(b1+", "+ b2 +" are different");
}
}

Choose the correct option(s) to fill in place of CODE BLOCK so that the output is:

ABC, ABC are same

Select all that apply.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 3

+4 marksOne or more correct options

Consider the code given below.

java
class Rectangle{
private int sides;
public Rectangle(int s){
sides = s;
}
public final void area(){
System.out.println("area of rectangle");
}
}
class Pentagon extends Rectangle {
public Pentagon(int n){
super(n);
}
public final void area(){ // LINE 1
System.out.println("area of pentagon");
}
}
public class Test{
public static void main(String[] args){
Rectangle r = new Pentagon(4); // LINE 2
r.sides = 5; // LINE 3
r.area();
}
}

Choose the correct option(s).

Select all that apply.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answers

  • A
  • C

Question 4

+4 marksOne or more correct options

Consider the code given below.

java
interface Moveable{
void move();
}
class Fan implements Moveable{
public void move() {
System.out.println("Move and circulate air inside the room");
}
}
class Cooler{
public Fan getBlower() {
return new Blower();
}
private class Blower extends Fan implements Moveable{
public void move() {
System.out.println("Move and throws air at high pressure");
}
}
}
public class PrivateTest {
public static void main(String[] args) {
//CODE BLOCK
}
}

Choose the correct option(s) to be filled in place of CODE BLOCK so that the output is:

Move and throws air at high pressure

Select all that apply.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answers

  • A
  • D

Question 5

+3 marksOne correct option
  1. A

    A-6, B-5, C-3, D-1, E-2

  2. B

    A-4, B-5, C-1, D-3, E-2

  3. C

    A-4, B-2, C-3, D-1, E-5

  4. D

    A-6, B-2, C-1, D-3, E-5

Show answer

Correct answer

  • B

    A-4, B-5, C-1, D-3, E-2

Question 6

+3 marksOne correct option

Consider the code given below.

java
class Vehicle{
public void move() {
System.out.println("Moves");
}
}
class Bike extends Vehicle{
public void seating() {
System.out.println("Bike seating");
}
}
class Car extends Bike{
public void seating() {
System.out.println("Car Seating");
}
public void safety() {
System.out.println("Offers safety");
}
}
public class Test{
public static void main(String[] args) {
Vehicle v = new Bike();
Bike b = new Car(); // LINE 1
v.move();
((Bike)v).seating(); // LINE 2
b.safety(); // LINE 3
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 7

+3 marksOne correct option

Consider the code given below.

java
class Player{
public void play(){
System.out.println("Player plays");
}
public void play(String n){
System.out.println("Player is " + n);
}
}
class Captain extends Player{
public void play(String s){
System.out.println("Captain is " + s);
}
}
public class Test{
public static void main(String[] args){
Player p = new Captain(); // LINE 1
p.play();
p.play("siva"); // LINE 2
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 8

+3 marksOne correct option

Consider the Java code given below.

java
class Employee{
private String name;
private double salary;
public Employee(String n, double s) {
name = n;
salary = s;
}
public String toString() {
return "name = " + name + ", salary = " + salary;
}
}
class Manager extends Employee{
String dateOfPromotion;
public Manager(String name, double salary, String dop) {
//LINE-1
dateOfPromotion = dop;
}
public String toString() {
return super.toString()+", dateOfPromotion = "+dateOfPromotion;
}
}
public class Test {
public static void main(String[] args) {
Employee obj = new Manager("Suyan", 500000.00, "01/06/2021");
System.out.println(obj);
}
}

Choose the correct option to be filled in place of LINE-1 so that the output is:

name = Suyan, salary = 500000.0, dateOfPromotion = 01/06/2021

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 9

+3 marksOne correct option

Consider the Java code given below.

java
public class SwitchTest {
public static void show() {
for(int i=0;i<3;i++) {
switch(i) {
case 1:
System.out.print("1 ");
case 2:
System.out.print("2 ");
case 3:
System.out.print("3 ");
default:
System.out.print("Default case");
break;
}
System.out.println();
}
}
public static void main(String[] args) {
show();
}
}

What will the output be?

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B

Question 10

+3 marksOne correct option

Consider the code given below.

java
abstract class Phone{
abstract void basic_features();
abstract void additional_features();
}
abstract class KeypadPhone extends Phone{ // LINE 1
public void basic_features() {
System.out.println("Calling/Messaging");
}
}
class SmartPhone extends KeypadPhone{ // LINE 2
public void additional_features() {
System.out.println("Touch screen/Camera");
}
}
public class Test {
public static void main(String[] args) {
KeypadPhone obj1 = new SmartPhone();
Phone obj2 = new SmartPhone();
obj1.basic_features();
obj2.additional_features(); // LINE 3
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 11

+3 marksOne correct option

Consider the code given below.

java
interface Appraisable{
public default void isAppraisable() {
System.out.println("Yes this employee appraisable");
}
}
class Employee{
int leaves;
public Employee(int num) {
leaves = num;
}
public void isAppraisable() {
if(leaves >= 20)
System.out.println("This employee not appraisable");
}
}
class Manager extends Employee implements Appraisable{
public Manager(int num) {
super(num);
}
}
public class InterfaceTest {
public static void main(String[] args) {
Appraisable obj1 = new Manager(10);
obj1.isAppraisable();
Appraisable obj2 = new Manager(20);
obj2.isAppraisable();
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • C

Question 12

+3 marksOne correct option

Consider the code given below.

java
class Doctor {
private String name = "****";
private static String hospital_group = "ABC";
public Doctor(){
name = "**";
}
public Doctor(String n){
name = n;
}
public String toString() {
return "name = " + name + ", hospital group = " + hospital_group;
}
}
public class VarTest{
public static void main(String[] args) {
Doctor obj1 = new Doctor();
Doctor obj2 = new Doctor("XYZ");
System.out.println(obj1);
System.out.println(obj2);
}
}

What will the output be?

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 13

+3 marksOne correct option

Consider the code given below.

java
class Person{
private String name, location;
private int age;
public void setLocation(String location) {
this.location = location;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person(String name, int age, String location) {
this.name = name;
this.age = age;
this.location = location;
}
public Person(Person p) {
this(p.name, p.age, "Hyderabad");
}
public String toString() {
return name + ", " + age + ", " + location;
}
}
public class CopyConTest {
public static void main(String[] args) {
Person p1 = new Person("ABC", 23);
Person p2 = new Person(p1);
p1.setLocation("Mumbai");
System.out.println(p1);
System.out.println(p2);
}
}

What will the output be?

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • D

Question 14

+3 marksOne correct option

Consider the Java code given below.

java
class Animal{
String name;
int legs;
public Animal(String n, int l){
name = n;
legs = l;
}
***--------------***
* CODE SEGMENT *
***--------------***
}
public class Test{
public static void main(String[] args){
Animal a1 = new Animal("Dogs", 4);
Animal a2 = new Animal("Birds", 2);
System.out.println(a1 + "\n" + a2);
}
}

Choose the correct option to fill in the CODE SEGMENT so that the output is:

Dogs : 4
Birds : 2

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • A

Question 15

+4 marksOne correct option

Consider the Java code given below.

java
interface Bookable{
public void booked();
public void rejected();
}
class TicketBookingSite{
public static int maxTickets = 100;
Bookable obj;
// Constructor to initialize Bookable obj
public void startBooking(int tickets) {
if(tickets <= maxTickets) {
obj.booked();
maxTickets -= tickets;
}
else
obj.rejected();
}
}
class Audience implements Bookable{
String name;
int noTickets;
// Constructor initialize name and noTickets
public void getTickets() {
TicketBookingSite site = new TicketBookingSite(this);
site.startBooking(noTickets);
}
public void booked() {
System.out.println("You got "+this.noTickets+" tickets");
}
public void rejected() {
System.out.println("Tickets sold out");
}
}
public class CallBack {
public static void main(String[] args) {
Audience a1 = new Audience("A", 34);
Audience a2 = new Audience("B", 67);
Audience a3 = new Audience("C", 34);
a1.getTickets();
a2.getTickets();
a3.getTickets();
}
}

Choose the correct option.

  1. A
  2. B
  3. C
  4. D
Show answer

Correct answer

  • B