Java Quiz

Question 1. Given this code fragment:

List<String> vitamins = new ArrayList<>();
vitamins.add("A");
vitamins.add("B12");
vitamins.add("C");
vitamins.set(1,"B");
vitamins.add(1,"D");
System.out.println(vitamins);

What is the result?

a. [A, D, B, C]

b. [A, D, C]

c. [D, B, C]

d. [D, B12, C]

Question 2. Given this code fragment:

1. class Engine { }
2. public class App {
3.     public static void main(String[] args) {
4.        Engine e = new Engine();
5.        Engine e1 = e;
6.        e = null;
7.     } 
8. }

Which statement is true about this code?

a. It creates an object and the object is eligible for garbage collection.

b. It creates an object and the object is not eligible for garbage collection.

c. It creates two objects: e and e1. The e object is eligible for garbage collection.

d. It creates two objects and both the objects are not eligible for garbage collection.

Question 3. Given this code fragment:

    double wage = 2.0;
    int weekDays = 5;
    long monDays = weekDays * 4;
    long yearDays = monDays * 12L;
    long totalWage = yearDays * wage; // line n4

Which modification enables the code to compile?

a. Replace line n1 with double wage = 2;
b. Replace line n2 with long monDays = weekDays * 4L;
c. Replace line n3 with long yearDays = monDays * 12;
d. Replace line n4 with long totalWage = yearDays * (long) wage;

Advertisements