Java Fundamentals: Types, Control Flow, and Classes

Test your knowledge of core Java programming concepts including primitive types, type casting, operators, constructors, class modifiers, threading, and inheritance.

10 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

HashMap is thread-safe whereas Hashtable is not10, Which of the following assignment statements is invalid?

  1. double D = 45456.444;
  2. long L = 45784;
  3. int I = L;
  4. int J = (int) D;
Question 2 Multiple Choice (Single Answer)

Assume the following method is properly synchronized and called from a thread A on an object B: wait(2000); After calling this method, when will the thread A become a candidate to get another turn at the CPU?

  1. After object B is notified, or after two seconds
  2. After the lock on B is released, or after two seconds
  3. Two seconds after object B is notified
  4. Two seconds after lock B is released.
Question 3 Multiple Choice (Single Answer)

for(Object obj: expression) { } What interface must the ?expression? implement in order for you to use it with enhanced for loop construct?

  1. Iterator
  2. Map
  3. Enumeration
  4. Iterable
Question 4 Multiple Choice (Single Answer)

Which of the following are primitive types?

  1. Float
  2. None of the above
  3. byte
  4. String
  5. integer
Question 5 Multiple Choice (Single Answer)

What will happen when you attempt to compile and run the following code


public class Hope{
    public static void main(String argv[]){
        Hope h = new Hope();
    }
    protected Hope(){
        for(int i =0;
        i <10;
        i ++){
            System.out.println(i);
        }
    }
}
  1. Compilation and running with output 0 to 9
  2. Run time error: Constructors cannot be declared protected
  3. Compilation error: Constructors cannot be declared protected
  4. Compilation and running with output 0 to 10
  5. None of the above
Question 6 Multiple Choice (Single Answer)

What is output of the above program?

Class conditional {
 public static void main(String args[]) {
  int i = 20;
  int j = 55;
  int z = 0;
  z = i < j ? i : j; // ternary operator
  System.out.println("The value assigned is " + z);
 }
}
  1. 20
  2. 55
  3. None of the above
Question 7 Multiple Choice (Single Answer)

A class cannot be declared:

  1. Static
  2. Private
  3. Default
  4. None of the above
Question 8 Multiple Choice (Single Answer)

Following code will result in:

int a1 = 5; 
double a2 = (float)a1;
  1. Compilation error
  2. Runtime error
  3. No errors
  4. Execptions
Question 9 Multiple Choice (Single Answer)

Following code will result in:

class A {
  int b = 1;
  public static void main(String [] args) {
       System.out.println("b is " + b);
 }
}
  1. Compilation error
  2. Runtime Error
  3. Runtime Exception
  4. Output of b is 1
Question 10 Multiple Choice (Single Answer)

Following code will result in:

class A { 
   public static void main(String [] args) {
       B b = new A(); 
   }
} 
class B extends A {}
  1. Compilation error
  2. Runtime Error
  3. Runtime Exception
  4. Other Execptions