Java Question Bank / FAQs
This is a mock Exam based on the Objectives for the Sun Java Programmers Exam. It is
created by Genesis InSoft Limited (admin@genesisinsoft.com)
and may be freely distributed so long as it is unmodified. Please email us if you have any
corrections or comments.
Question 1
The Java disassembler option, which is used to disassemble the source
code and display the byte code produced by the compiler is
- javap -dc
- javap -a
- javap -c
- javap -l
Answer to Question 1
Question 2
What is the output of the program?
outer:for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.println
("i = " + i + " j = " + j);
if (i == j)
{
continue outer;
}
}
}
- I=0 j=0 , I=0 j=0 and I=0 j=2
- I=0 j=0 , I=0 j=1 and I=1 j=2
- I=0 j=0 , I=1 j=0 and I=1 j=1
- I=0 j=1 , I=0 j=2 and I=2 j=0
Answer to Question 2
Question 3
Consider the following code:
1. public class Test3 {
2. static int total = 10;
3. public static void main (String args []) {
4. new Test3();
5. }
6. public Test3() {
7.
System.out.println("In
test");
8.
System.out.println(this);
9. int temp =
this.total;
10. if (temp > 5) {
11.
System.out.println(temp);
12. }
13. }
14. }
What is the output of the above program?
- The compiler reports and error at line 2.
- The compiler reports an error at line 9.
- The value 10 is one of the elements printed to the standard output.
- The class compiles but generates a runtime error
Answer to Question 3
Question 4
What is the output of the program?
public class Xor {
public static void main(String args[]) {
byte b = 10;
byte c = 15;
b = (byte)(b ^ c);
System.out.println (b);
}
}
- 25
- 5
- 10
- 15
Answer to Question 4
Question 5
True or False: Readers have methods that can read and return floats and
doubles.
- True
- False
Answer to Question 5
Question 6
The "System.out" object used for the standard input oprations is instantiated
from which class?
- System class
- OutputStream class
- PrintStream class.
- Object class
Answer to Question 6
Question 7
Does the code fragment below compile successfully? If so, is line 2
executed?
if ("Hedgehog".startsWith("Hedge"))
System.out.println("Line 2");
- code does not compile
- code compiles, line 2 does not executed
- code compiles, line 2 executed
- none
Answer to Question 7
Question 8
public class Base {
public int method(int i) {
System.out.println("Value is " + i);
return 0;
}
}
public class Sub extends Base {
public int method(int j) {
return (method("method("+j+")"));
}
public int method(String s) {
System.out.println("I was passed " +
s);
return s.length();
}
public static void main(String args[]) {
Base b1 = new Base();
Base b2 = new Sub();
b2.method(b1.method(b2.method(5)));
}
}
What is the output when the main method of the class Sub is run?
- I was passed method(5), value is (9), I was passed method(0)
- I was passed method(5), value is (9), I was passed method(5)
- I was passed method(0), value is (9), I was passed method(5)
- I was passed method(5), value is (0), I was passed method(5)
Answer to Question 8
Question 9
One Java application can have more than one main () method.
State which following statement is correct?
- We can not put more than one main method
- We can put more than one main method, but all methods should have same signature
- We can put more than one main method, but all methods should have different signature
- We can put more than one main method and signatures can same or different
Answer to Question 9
Question 10
What is the output when the main method of the class test is run?
public class test{
public static void main(String arg[])
{
int a = 5;
a+=++a;
System.out.print (a);
}
}
- 12
- 11
- 10
- 13
Answer to Question 10
Question 11
Consider the following code:
public class prg {
static int A;
public static void main(String a[]) {
boolean x;
if((x=(++A==0)?
false:true))
if((x=(++A==0)? true: false))
System.out.println (++A);
else
System.out.println(A + 10);
else
System.out.println(A + 20);
}
}
What is the output when the main method of the class test is run?
- 2
- 20
- 11
- 12
Answer to Question 11
Question 12
Consider the following code:
public class prg {
public static void main(String a[]) {
switch ('\0')
{
case '0': System.out.print(" 0 ");
case '1': System.out.print(" 1 ");
}
}
}
What is the output when the main method of the class test is run?
- 0
- 0 1
- no output
- compilation error
Answer to Question 12
Question 13
Consider the following code
public class prg {
static int cal(int x, int y) {
return(x + 2 * y);
}
public static void main(String a[]) {
int x = 0, y = 2;
System.out.println (cal
(x += 5, y = x = 9));
}
}
What is the output when the main method is run?
- 32
- 19
- 23
- 18
Answer to Question 13
Question 14
Comment on the following declarations?
String str;
String str=" " ;
- both are same and both refer to null.
- they are different with the first declaration as a null String Object and the second one
as null String.
- they are different with the difference that the second one is instantiated and
initialised to null and the first one is instantiated and refers to some garbage value.
- None of the above.
Answer to Question 14
Question 15
Consider the following code:
public class Xor {
public static void main(String args[]) {
final int x=4;
if ( x++ != 0 )
System.out.print
("x val "+x);
}
}
What is the output when the main method of the class test is run?
- can be compiled , no output
- can be compiled , output is 5
- compilation error
- no output
Answer to Question 15
Question 16
Consider the following code:
class xyz {
public String myname;
}
public class test {
public static void main(String args[])
{
xyz a = new
xyz();
xyz b = a;
b.myname =
"newname";
System.out.print(" str1 "+a.myname+" str2 "+b.myname);
}
}
What is the output when we run the above program?
- newname, newname
- no value, newname
- no output
- newname, no value
Answer to Question 16
Question 17
Which one statement is true about the code fragment below?
import java.lang.Math;
public class math {
public static
void main(String arg[] ){
Math myMath =
new Math();
System.out.println("cosine of 0.123 = " + myMath.cos(0.123));
}
}
- compilation error line no 4
- compilation error line no 5
- no compilation error, during the execution, an exception is thrown at line 5
- no compilation error and no exception is thrown and display the output
Answer to Question 17
Question 18
What does the access Specifier " private protected " mean ?
- The method is accessible by the class only.
- The method is accessible by the class and its subclasses but the instances of the class
or its
subclass cannot.
- The method is accessible by the class and classes which are available in the same
package.
- There is no such access specifier
Answer to Question 18
Question 19
What is the ouput when the following code is executed?
public class Short {
public static void main(String args[]) {
StringBuffer s = new
StringBuffer("Hello");
if((s.length() > 5)
&& (s.append(" there").equals("False"))) ;
System.out.println("value is " + s);
}
}
- The output: value is Hello
- The output: value is Hello there
- compile error at line no 4
- Null pointer exception
Answer to Question 19
Question 20
Which of the following determines if the specified character is a
"Java" letter or digit, that is, the character is permissible as a non-initial
character in an identifier in the Java language.
- isLetterorDigit( char )
- isletterOrdigit( char );
- isJavaLetterOrDigit( char )
- isJavaletterOrDigit( char )
Answer to Question 20
Answers
Answer 1 - C
Back to question 1
Answer 2 - C
Back to question 2
Answer 3 - C
Back to question 3
Answer 4 - B
Back to question 4
Answer 5 - A
Back to question 5
Answer 6 - C
Back to question 6
Answer 7 - C
Back to question 7
Back to question 8
Answer 9 - D
Back to question 9
Answer 10 - B
Back to question 10
Answer 11 - D
Back to question 11
Answer 12 - C
Back to question 12
Answer 13 - C
Back to question 13
Answer 14 - B
Back to question 14
Answer 15 - C
Back to question 15
Answer 16 - A
Back to question 16
Answer 17 - A
Back to question 17
Answer 18 - B
Back to question 18
Answer 19 - A
Back to question 19
Answer 20 - C
Back to question 20
|