uiz Space

January 2024 term · Programming Concepts using Java · BSCS2005

Programming Concepts using Java Quiz 1: 25 February 2024 (January 2024 term)

The IIT Madras BS Programming Concepts using Java (Java) Quiz 1 paper sat on 25 Feb 2024, in the January 2024 term: 15 questions for 100 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
100
Duration
120 min
MSQ
2
MCQ
13

Updated

Official paper: IIT M DIPLOMA AN2 EXAM QDD2 25 Feb 2024 · No negative marking.

Question 1

+6 marksOne or more correct options

Which of 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 said to be in scope.

  2. B

    The lifetime of a variable is the duration that it spends in the topmost activation record in the stack.

  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

+6 marksOne or more correct options

Select all that apply.

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

Correct answers

  • A
  • C

Question 3

+6 marksOne correct option

Consider the code given below.

java
class Athlete {
public void perform() {
System.out.println("Athlete performs");
}
public void perform(String activity) {
System.out.println("Athlete performs " + activity);
}
}
class Gymnast extends Athlete {
public void perform(String move) {
System.out.println("Gymnast performs " + move);
}
}
public class TestAthlete {
public static void main(String[] args) {
Athlete a = new Gymnast(); // LINE 1
a.perform();
a.perform("backflip"); // LINE 2
}
}

Choose the correct option.

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

Correct answer

  • D

Question 4

+6 marksOne correct option

Consider the Java code given below.

java
class Vehicle {
public final void startEngine() {
System.out.println("Starting the engine of a vehicle");
}
}
class Car extends Vehicle {
public final void startEngine() {
System.out.println("Starting the engine of a car");
}
}
public class TestVehicles {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.startEngine();
}
}

Choose the correct option regarding the given code.

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

Correct answer

  • D

Question 5

+6 marksOne correct option

Consider the Java code given below.

java
interface Readable {
default void read() {
System.out.println("reads");
}
}
class Book implements Readable { //LINE 1
}
class Newspaper implements Readable {
public void read() {
System.out.println("reads news");
}
}
public class Test {
public static void main(String[] args) {
Readable r1 = new Book();
r1.read(); // LINE 2
Readable r2 = new Newspaper();
r2.read();
}
}

Choose the correct option.

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

Correct answer

  • B

Question 6

+6 marksOne correct option

Consider the Java code given below.

java
interface Playable {
default void play() {
System.out.println("Plays");
}
}
interface Pauseable {
default void pause() {
System.out.println("Pauses");
}
}
class MediaPlayer implements Playable, Pauseable{ //LINE 1
public void play(){
System.out.println("Plays media player");
}
}
public class Test {
public static void main(String[] args) {
Playable p1 = new MediaPlayer();
p1.play();
p1.pause(); //LINE 2
}
}

Choose the correct option.

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

Correct answer

  • B

Question 7

+6 marksOne correct option

Consider the code given below.

java
interface PaymentMethod {
void processPayment();
}
class Payment {
public CreditCard getPaymentMethod1() {
return new CreditCard();
}
public PayPal getPaymentMethod2() {
return new PayPal();
}
private class CreditCard implements PaymentMethod {
public void processPayment() {
System.out.println("Processing credit card payment");
}
}
private class PayPal extends CreditCard {
public void processPayment() {
System.out.println("PayPal payment started");
}
}
}
public class PrivateTest {
public static void main(String[] args) {
Payment p = new Payment();
// CODE BLOCK
obj1.processPayment();
obj2.processPayment();
}
}

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

Processing credit card payment
PayPal payment started

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

Correct answer

  • B

Question 8

+7 marksOne correct option

Consider the Java code given below.

java
public class PatternSwitchExample {
public static void displayPattern(int value) {
switch (value) {
case 1:
System.out.print("A ");
break;
case 2:
System.out.print("B ");
break;
case 3:
System.out.print("C ");
break;
default:
System.out.print("X ");
break;
}
}
public static void main(String[] args) {
for (int i = 3; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
displayPattern(i);
}
System.out.println();
}
}
}

What will the output be?

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

Correct answer

  • C

Question 9

+7 marksOne correct option

Consider the Java code given below.

java
class Student {
private String name;
private int age;
public Student(String n, int a) {
name = n;
age = a;
}
public Student(Student s) {
this.name = s.name;
this.age = s.age;
}
public void setName(String n) {
name = n;
}
public void setAge(int a) {
age = a;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student("Alice", 20);
Student s2 = s1;
s1.setAge(25);
s1.setName("Emily");
System.out.println("s2:name: " + s2.getName() + ", age: " + s2.getAge());
}
}

What will the output be?

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

Correct answer

  • A

Question 10

+7 marksOne correct option

Consider the Java code given below.

java
class Car {
private String model;
private int year;
public Car(String m, int y) {
model = m;
year = y;
}
***--------------***
* CODE SEGMENT *
***--------------***
}
public class CarTest {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 2022);
Car car2 = new Car("Ford", 2020);
System.out.println(car1 + "\n" + car2);
}
}

Choose the correct option to fill in the CODE SEGMENT so that the output is:
Toyota : 2022
Ford : 2020

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

Correct answer

  • A

Question 11

+7 marksOne correct option

Consider the following code:

java
class Animal {
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
public void makeSound(String s) {
makeSound();
System.out.println("Dog " + s);
}
}
public class TestAnimals {
public static void main(String[] args) {
Animal myPet = new Dog();
//CODE SEGMENT
}
}

Choose the correct option to fill in the CODE SEGMENT so that the output is:
Animal makes a sound
Dog barks

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

Correct answer

  • D

Question 12

+7 marksOne correct option

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

java
class Person {
private String name;
private int age;
// Constructor to initialize instance variables
public String toString() {
return name + " (" + age + " years old)";
}
public boolean equals(Object obj) {
//CODE BLOCK
}
}
public class TestPerson {
public static void main(String[] args) {
Person person1 = new Person("Alice", 25);
Person person2 = new Person("Alice", 25);
if (person1.equals(person2)){
System.out.println(person1 + " and " + person2 + " are the same");
}
else{
System.out.println(person1 + " and " + person2 + " are different");
}
}
}

Choose the correct option to fill in place of CODE BLOCK so that the output is:
Alice (25 years old) and Alice (25 years old) are the same

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

Correct answer

  • C

Question 13

+7 marksOne correct option

Consider the Java code given below.

java
1 interface Switchable {
2 public void turnOn();
3 public void turnOff();
4 }
5 interface SpeedAdjustable {
6 public void adjustSpeed();
7 default void displaySpeed() {
8 System.out.println("Speed is adjustable.");
9 }
10 }
11 class Fan implements Switchable,SpeedAdjustable {
12 public void turnOn() {
13 System.out.println("Fan is turned on.");
14 }
15 public void turnOff() {
16 System.out.println("Fan is turned off.");
17 }
18 }

Choose the correct option regarding the above code.

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

Correct answer

  • C

Question 14

+8 marksOne correct option

Consider the code given below.

java
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Rectangle extends Shape {
public void draw() {
System.out.println("Drawing a rectangle");
}
}
class Square extends Rectangle {
public void draw() {
System.out.println("Drawing a square");
}
public void color() {
System.out.println("Square is colorful");
}
}
public class TestShapes {
public static void main(String[] args) {
Shape s = new Rectangle();
Rectangle r = new Square(); // LINE 1
s.draw();
r.color(); // LINE 2
}
}

Choose the correct option.

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

Correct answer

  • C

Question 15

+8 marksOne correct option

Consider the Java code given below.

java
interface Discountable {
public default void isDiscountable() {
System.out.println("Item eligible for discount");
}
}
class Product {
double price;
public Product(double price) {
this.price = price;
}
public void isDiscountable() {
if (price <= 500.0) {
System.out.println("Item not eligible for discount");
}
else {
System.out.println("Discount granted");
}
}
}
class SaleItem extends Product implements Discountable {
public SaleItem(double price) {
super(price);
}
}
public class TestClass {
public static void main(String[] args) {
Discountable i1 = new SaleItem(600.0);
i1.isDiscountable();
Discountable i2 = new SaleItem(300.0);
i2.isDiscountable();
}
}

Choose the correct option.

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

Correct answer

  • C