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.
Questions
HashMap is thread-safe whereas Hashtable is not10, Which of the following assignment statements is invalid?
- double D = 45456.444;
- long L = 45784;
- int I = L;
- int J = (int) D;
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?
- After object B is notified, or after two seconds
- After the lock on B is released, or after two seconds
- Two seconds after object B is notified
- Two seconds after lock B is released.
for(Object obj: expression) { } What interface must the ?expression? implement in order for you to use it with enhanced for loop construct?
- Iterator
- Map
- Enumeration
- Iterable
Which of the following are primitive types?
- Float
- None of the above
- byte
- String
- integer
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);
}
}
}
- Compilation and running with output 0 to 9
- Run time error: Constructors cannot be declared protected
- Compilation error: Constructors cannot be declared protected
- Compilation and running with output 0 to 10
- None of the above
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);
}
}
- 20
- 55
- None of the above
A class cannot be declared:
- Static
- Private
- Default
- None of the above
Following code will result in:
int a1 = 5;
double a2 = (float)a1;
- Compilation error
- Runtime error
- No errors
- Execptions
Following code will result in:
class A {
int b = 1;
public static void main(String [] args) {
System.out.println("b is " + b);
}
}
- Compilation error
- Runtime Error
- Runtime Exception
- Output of b is 1
Following code will result in:
class A {
public static void main(String [] args) {
B b = new A();
}
}
class B extends A {}
- Compilation error
- Runtime Error
- Runtime Exception
- Other Execptions