Programming Concepts using Java, Quiz 2
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).
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.
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). 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. Consider two Java files located in two different packages as shown below. A.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 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. Consider the Java code given below. 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