Likes

Java Objective Questions and Answer


QUESTION NO: 1
Given:
1. public class Test {
2. public static void main(String args[]) {
3. class Foo {
4. public int i = 3;
5. }
6. Object o = (Object)new Foo();
7. Foo foo = (Foo)o;
8. System.out.println(“i = “ + foo.i);
9. }
10. }

What is the result?
A. i = 3
B. Compilation fails.
C. A ClassCastException is thrown at line 6.
D. A ClassCastException is thrown at line 7.
Answer: A

QUESTION NO: 2
Which two cause a compiler error? (Choose two)
A. float[] = new float(3);
B. float f2[] = new float[];
C. float[] f1 = new float[3];
D. float f3[] = new float[3];
E. float f5[] = { 1.0f, 2.0f, 2.0f };
F. float f4[] = new float[] { 1.0f. 2.0f. 3.0f};
Answer: A, B
The F. statement is incorrect. The float numbers should be separated with commas and not
dots.

QUESTION NO: 3 
Given:
11. int i =1,j =10;
12. do {
13. if(i++> --j) {
14. continue;
15. }
16. } while (i <5);
17. System.out.println(“i = “ +i+ “and j = “+j);
What is the result?
A. i = 6 and j = 5
B. i = 5 and j = 5
C. i = 6 and j = 5
D. i = 5 and j = 6
E. i = 6 and j = 6
Answer: D 

QUESTION NO: 4

Given:
1. class Test {
2. private Demo d;
3. void start() {
4. d = new Demo();
5. this.takeDemo(d);
6. }
7.
8. void takeDemo(Demo demo) {
9. demo = null;
10. demo = new Demo();
11. }
12. }

When is the Demo object, created on line 3, eligible for garbage collection?
A. After line 5.
B. After line 9.
C. After the start() method completes.
D. When the takeDemo() method completes.
E. When the instance running this code is made eligible for garbage collection.
Answer: E

.Sun JAVA2 Certification Program Volume 1.0 

Q1.Which of the following are compiling without warning or error?
a) float f=1.3; 
b) char c="a";
c) byte b=257;
d) boolean b=null;
e) int i=10;

Q2.Which of the following are legal statements?
a) float f=1/3;
b) int i=1/3;
c) float f=1.01;
d) double d=999d;. 

Q3.Which of the following are Java keywords?
a) NULL.
b) new.
c) instanceOf.
d) wend.

Q4.Which of the following are valid statements?
a) System.out.println(5+4);
b) int i=2+'3';
c) String s="one"+'Two';
d) byte b=255; 

Q 5.Which of the following statements are true?
a) The garbage collection algorithm in Java is vendor implemented.
b) The size of primitives is platform dependent.
c) The default type for a numerical literal with decimal component is a float.
d) You can modify the value in an Instance of the Integer class with the setValue method.


Q6.Which of the following are true statements?
a) The creation of a named instance of the File class creates a matching file in the underlying operating system only when the close method is called.
b) The RandomAccessFile class allows you to move directly to any point a file.
c). I/O in Java can only be performed using the Listener classes.
d) The characteristics of an instance of the File class such as the directory separator, depend on the current underlying operating system.

Q 7.Which of the following statements are true?
a) The instanceof operator can be used to determine if a reference is an instance of a class, but not an interface.
b) The instanceof operator can be used to determine if a reference is an instance of a particular primitive wrapper class.
c) The instanceof operator will only determine if a reference is an instance of a class immediately above in the hierarchy but no further up the inheritance chain.
d) The instanceof operator can be used to determine if one reference is of the same class as another reference thus.

Q8.Which of the following statements are true?
a) An interface can only contain method and not variables.
b) Interfaces cannot have constructors.
c) A class may extend only one other class and implement only one interface.
d) Interfaces are the Java approach to addressing its lack of multiple inheritance, but require implementing classes to create the functionality of the Interfaces.

Q9.Which of the following are valid statements? 
a) public class MyCalc extends Math
b) Math.max(5);
c) Math.round(9.99,1);
d)Math.mod(4,10);

Q10.Which of the following are methods of the Runnable interface?
a) run
b) start
c) yield
d) stop 

Q11.Which of the following statements are true?
a) A byte can represent between -128 to 127.
b) A byte can represent between -127 to 128. 
c) A byte can represent between -256 to 256. 
d) A char can represent between -2x2 pow 16 2 x2 pow 16 - 1. 

Q12.What will happen when you attempt to compile and run the following code?
class Base{
public void Base(){
System.out.println("Base");
}
}
public class Inner extends Base{
public static void main(String argv[]){
Inner i=new Inner();
}
}
a) Compile time error Base is a keyword.
b) Compilation and no output at runtime.
c) Output of Base.
d) Runtime error Base has no valid constructor. 
Q13.You have a public class called myclass with the main method defined as follows
public static void main(String parm[]) { 

System.out.println(parm[0]);
If you attempt to compile the class and run the program as follows
“java myclass hello”
What will happen?
a) Compile time error, main is not correctly defined.
b) Run time error, main is not correctly defined.
c) Compilation and output of java.
d) Compilation and output of hello.

Q14.Which of the following statements are true?
a) If a class has any abstract methods it must be declared abstract itself.
b) All methods in an abstract class must be declared as abstract.
c) When applied to a class, the final modifier means it cannot be sub-classed.
d) transient and volatile are Java modifiers. 

Q15.Which of the following are valid methods?
a) public static native void amethod(){}
b) public static void amethod(){}
c) private protected void amethod(){}
d) static native void amethod();

Q16.Which of the following statements are true?
a) Constructors cannot have a visibility modifier.
b) Constructors can be marked public and protected, but not private.
c) Constructors can only have a primitive return type.
d) Constructors are not inherited.

Q17.What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Inner extends Base{
public static void main(String argv[]){
Inner s = new Inner();
}
void Inner (){
System.out.println("Inner");
}
}
a) Compilation and output of the string “Inner “at runtime.
b) Compile time error.
c) Compilation and no output at runtime.
d) Compilation and output of the string "Base".

Q18.Which of the following statements are true?
a) A static methods do not have access to the implicit variable called this.
b) A static method may be called without creating an instance of its class.
c) A static method may not be overridden to be non-static.
d) A static method may not be overloaded. 

Q19.Which of the following will compile without error?
a)char c='1';
System.out.println(c>>1);
b)Integer i=Integer("1");
System.out.println(i>>1);
c)int i=1;
System.out.println(i<<<1);
d)int i=1;
System.out.println(i<<1);

Q 20.Which of the following are Java keywords?
a) sizeof
b) main
c) transient
d) volatile 

Q21.Which of the following statements are true? 
a) The default constructor has a return type of void.
b) The default constructor takes a parameter of void.
c) The default constructor takes no parameters.
d) The default constructor is not created if the class has any constructors of its own. 

Q22.Which of the following statements are true?
a) All of the variables in an interface are implicitly static.
b) All of the variables in an interface are implicitly final.
c) All of the methods in an interface are implicitly abstract.
d) A method in an interface can access class level variables.

Q 23.Which of the following statements are true?
a) The String class is implemented as a char array, elements are addressed using the stringname[] convention.
b) The + operator is overloaded for concatenation for the String class.
c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type.
d) The size of a string can be retrieved using the length property.

Q 24.Which of the following statements are true?
a) A method in an interface must not have a body.
b) A class may extend one other class plus at most one interface.
c) A class may extends at most one other class plus implement many interfaces.
d) An class accesses an interface via the keyword uses. 
Q 25.Which of the following statements are true?
a) The following statement will produce a result of 1. 
System.out.println( -1 >>>2);
b) Performing an unsigned left shift (<<<) on a negative number will always produce a negative number result.
c) The following statement will produce a result of zero, 
System.out.println(1 >>1);
d) All the Java integral types are signed numbers




No comments: