Quiz Space

Java Quiz 2: 24 March 2024 (January 2024 term)

Question 1

+6 marksOne correct option

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).

java
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.

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

Question 2

+6 marksOne correct option

Consider two Java files located in two different packages as shown below.

A.java:

java
package com.pack1;
public class A {
void methodOne() {
System.out.println("Display methodOne");
}
private void methodTwo() {
System.out.println("Display methodTwo");
}
protected void methodThree() {
System.out.println("Display methodThree");
}
public void methodFour() {
System.out.println("Display methodFour");
}
}

B.java

java
package com.pack2;
import com.pack1.A;
public class B extends A {
public static void main(String[] args) {
B obj = new B();
obj.methodOne(); //LINE 1
obj.methodTwo(); //LINE 2
obj.methodThree(); //LINE 3
obj.methodFour(); //LINE 4
}

Choose the correct option.

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

Question 3

+6 marksOne correct option

Consider the Java code given below.

java
class Validation {
public boolean validate(int a, int b) {
assert a > 0: "a should be greater than zero"; //LINE 1
assert b >= 1: "b should not be less than one"; //LINE 2
return true;
}
}
public class AssertTest {
public static void main(String[] args) {
int a = 1;
int b = -1;
int result = 0;
Validation obj = new Validation();
if (obj.validate(a, b))
result = a / b;
assert result > 0: "result should be greater than zero"; //LINE 3
System.out.println(result);
}
}

Choose the correct option when the program is executed as:
java -ea AssertTest

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

12 more questions in this paper

Sign in with Google — it is free — to see every question with its answer and explanation, practise it in learning mode, or take it as a timed mock test.

More on the Java Quiz 2 24 Mar 2024 paper

The IIT Madras BS Programming Concepts using Java (Java) Quiz 2 paper sat on 24 Mar 2024, in the January 2024 term: 15 questions for 100 marks in 120 minutes. The first 3 questions are below. Sign in with Google — it is free — to see the whole paper with its answers and explanations, in learning mode or as a timed mock test.

FeatureJava Quiz 2 24 Mar 2024 at a glance
TermJanuary 2024 term
SubjectProgramming Concepts using Java
Course codeBSCS2005
Questions15
Marks100
Duration120 min
MCQ12
MSQ3
Official paperIIT M DIPLOMA AN EXAM QDD2 24 Mar 2024
Negative markingNo negative marking.
Updated

Same Quiz 2, other subjects

More Java