Pretty simple question. What would happen if you try to execute the
below program?
public Class States{
private String state;
public States(String state){
this.state = state;
}
public String getState(){
return state;
}
public void setState(String state){
this.state = state;
}
public void clearState(){
this = null;
}
public static void main(String args[]){
States s = new States(“Active”);
s.clearState();
}
}
Consider the below classes.
public class AH<E> {
private E a;
public void setA (E x) {
a = x;
}
public E getA () {
return a;
}
}
public class A {
}
public class C extends A {
}
public class D extends A {
}
For the following code snippets, identify whether the code:
* fails to compile,
* generates an error at runtime, or
* none of the above (compiles and runs without problem.)
a. AH<A> h = new AH<C>();
b. AH<D> h = new AH<A>();
c. AH<?> h = new AH<C>();
h.setA(new C());
d. AH h = new AH();
h.setA(new D());
e. AH<? extends A> h = new AH<C>();
h.setA(new C());
Consider the below generic method:
public static <U> void fillBoxes(U u, List<Box<U>> boxes) {
for (Box<U> box : boxes) {
box.add(u);
}
}
Also, this snippet,
Crayon red = ...;
List<Box<Crayon>> crayonBoxes = ...;
Now,
What is the way to invoke the generic method?
a. Box.<Crayon>fillBoxes(red, crayonBoxes);
b. Box.fillBoxes(red, crayonBoxes);
Is it possible in java to define a class with in an Interface? If yes,
what is the scope of that class? If No, Why is it not allowed in Java?
Why will this be useful for anyone?
Given an Main program, how do you make sure that some piece of code is
executed even if the user interrupts the execution of the program by
closing or pressing CTRL + C , say in the command prompt.
Can classes implementing Singleton pattern be extended? When is it
possible to have multiple instances of Singleton objects assuming that
the singleton has been implemented with threads safety?
What is Multiton pattern?
How do you write a java program that is run on a command prompt and
which displays a progress as:
Status x of 100%
Without going to the nextline. i.e only x keeps changing from 1 to 100
on the same line.