Share on Google+Share on Google+

SCJP Module-1 Question-1

Advertisement
This page gives you some example sample questions for SCJP examination preparation. These example is very helpful for SCJP preparation.

Given a sample code:

1.    public class Example1 {
2.   
3.    public static void main(String[] args) {
4.   
5.    System.out.println("To print x you need " + x);
    }
}

Which of these following given statement allow this program to compile and execute?

(1) "char x;" placed on line 2
(2) "char x;" placed on line 4
(3) "char x = 'g';" placed on line 2
(4) "char x = 'g';" placed on line 4
(5) "static char x='g';" placed on line 2
(6) "char x = new char();" placed on line 4

Answer-1

(4),(5)

Explanation:

Only statement 4 and 5 allow program to execute successfully and rest fails because of following reasons.

(1) : Cannot make a static reference to the non-static field x

(2): The local variable x may not have been initialized.

(3): Cannot make a static reference to the non-static field x.

(6): Syntax error on token "char", invalid ClassType

 

Advertisements

Posted on: July 5, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.