Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

JAVA Programming :: Objects and Classes

Home > JAVA Programming > Objects and Classes > General Questions

21. What is the output for the below code?

import javutil.LinkedList;
import javutil.Queue;
public class Test {
public static void main(String... args) {
Queue q = new LinkedList();
q.add("newyork");
q.add("ca");
q.add("texas");
show(q);
}
public static void show(Queue q) {
q.add(new Integer(1);
while (!q.isEmpty ( ) )
System.out.print(q.poll() + " ");
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

" q was originally declared as Queue, But in show() method it is passed as an untyped Queue. nothing in the compiler or JVM prevents us from adding an Integer after that. If the show method signature is public static void show(Queue q) than you
can't add Integer, Only String allowed. But public static void show(Queue q) is untyped Queue so you can add Integer. Y poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.

Workspace

Tags:

No Tags on this question yet!

22. Consider the following code. What is the Output:

public class StaticTest {
static {
System.out.println("Planet");
}
public static void main(String argv[]) {
}
static {
System.out.print("Welcome");
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

Here is no explanation for this answer

Workspace

Tags:

No Tags on this question yet!

23. What is the output ?

public class Test {
public static void main(String... args) {
Pattern p = Pattern.compile("a+b?c*");
Matcher m = p.matcher("ab");
boolean b = m.matches();
System.out.println(b);
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

X? X, once or not at all X* X, zero or more times X+ X, one or more times

Workspace

Tags:

No Tags on this question yet!

24. class A {
A(String s) {
}
A() {
}
}
class B extends A {
B() { }
B(String s) {
super(s);
}
void test() {
// insert code here
}
}
Which of the below code can be insert at line 7 to make clean compilation ?

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

A a = new B(); is correct because anonymous inner classes are no different from any other class when it comes to polymorphism.

Workspace

Tags:

No Tags on this question yet!

25. What is the output for the below code ?

public class A {
public void printValue(){
System.out.println("A");
}
}
public class B extends A {
public void printValue(){
System.out.println("B");
}
}
1. public class Test {
2. public static void main(String... args) {
3. A b = new B();
4. newValue(b);
5. }
6. public static void newValue(A {
7. if(a instanceof B){
8. ((B).printValue();
9. }
10. }
11. }

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option B

Explanation:

instanceof operator is used for object reference variables to check whether an object is of a particular type. In newValue(b); b is instance of B So works properly.

Workspace

Tags:

No Tags on this question yet!

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option D

Explanation:

Here is no explanation for this answer

Workspace

Tags:

No Tags on this question yet!

27. What is the output for the below code ?

public class Test {
static int i =5;
public static void main(String... args) {
System.out.println(i++);
System.out.println(i);
System.out.println(++i);
System.out.println(++i+i++);

}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option A

Explanation:

i++ : print value then increment (postfix - increment happens after the value of the variable is used) ++i : increment the print (prefix - increment happens before the value of the variable is used

Workspace

Tags:

No Tags on this question yet!

28. What gets printed when the following code is compiled and run with the following command –
java test 2
Select the one correct answer.

public class test {
public static void main(String args[]) {
Integer intObj=Integer.valueOf(args[args.length–1]);
inti = intObj.intValue();
if(args.length> 1)
System.out.println(i);
if(args.length> 0)
System.out.println(i – 1);
else
System.out.println(i – 2);
}
}

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option D

Explanation:

Note that the program gets one command line argument – 2. args.length will get set to 1. So the condition if(args.length > 1) will fail, and the second check if(args.length > 0) will return true.

Workspace

Tags:

No Tags on this question yet!

29. Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C). Select the one correct answer.

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option E

Explanation:

Here is no explanation for this answer

Workspace

Tags:

No Tags on this question yet!

30. class HappyGarbage01
{
public static void main(String args[])
{
HappyGarbage01 h = new HappyGarbage01();
h.methodA(); /* Line 6 */
}
Object methodA()
{
Object obj1 = new Object();
Object [] obj2 = new Object[1];
obj2[0] = obj1;
obj1 = null;
return obj2[0];
}
}
Where will be the most chance of the garbage collector being invoked?

View Answer | Discuss in Forum | Workspace | Asked In |

Answer: Option D

Explanation:

Garbage collection takes place after the method has returned its reference to the object. The method returns to line 6, there is no reference to store the return value. so garbage collection takes place after line 6.

Workspace

Tags:

No Tags on this question yet!