This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes New java questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "New java questions" Watch "New java questions" New topic
Author

New java questions

ElanKumaran Srinivasan
Greenhorn

Joined: Sep 02, 2008
Posts: 3
Hi all,

The following are some sample 310-055 (Java 5.0) questions.
The difficulty level is moderate-difficult.

Have fun answering them.
Please do reply on how good/bad you found the questions to be/


Q1 . Given the following


1 class base1

2 {

3 void display()

4 {

5 System.out.println("Base display");

6 }

7 }

8

9 class student extends base1

10 {

11 void display()

12 {

13 System.out.println("student display");

14 }

15

16 void details()

17 {

18 System.out.println("student details");

19 }

20 }

21

22 public class teacher extends student

23 {

24 void display()

25 {

26 System.out.println("teacher display");

27 }

28

29 public void details()

30 {

31 System.out.println("Teacher details");

32 }

33 public static void main(String args[])

34 {

35 student s1=new student();

36 s1.details();

37 teacher t=new teacher();

38 t.details();

39 s1=t;

40 s1.details();

41 base1 b1=new base1();

42 b1=s1;

43 b1.display();

44 b1.details();

45 }

46 }



What is the result?

A) Student details

teacher details

student details

base display

student details

B) Student details

teacher deatails

teacher details

teacher display

student details

C) Student details

teacher deatils

student display

teacher display

teacher details

D) Student details

teacher details

teacher details

teacher display

teacher display

E) Compilation error

F) Exception thrown at runtime.


************************************************************


Q2. Given the following :

1 public class temp

2 {

3 static int x=0;

4 temp(int x)

5 {

6 x=x+1;

7 }

8 temp()

9 {

10 x++;

11 }

12

13 public static void main(String args[])

14 {

15 temp t=new temp(10);

16 System.out.println("The value of static variable is "+temp.x);

17 temp t2=new temp();

18 System.out.println("The value of static variable is "+temp.x);

19 }

20 }



What is the result ?

A) 11

12

B) 10

11

C) 11

11

D) 11

1

E) 0

1


*******************************************


Q19 Given the following

1 class game

2 {

3 protected static void play()

4 {

5 System.out.println("play game ");

6 }

7 }

8

9 class hockey extends game

10 {

11

12 public static void play()

13 {

14 System.out.println("play hockey");

15 }

16

17 }

18

19 public class foot extends hockey

20 {

21

22 public static void play()

23 {

24 System.out.println("play football");

25 }

26

27 public static void main(String args[])

28 {

29 foot f=new foot();

30 f.play();

31 hockey h=new hockey();

32 h.play();

33 h=f;

34 h.play();

35

36 }

37

38

39 }




What is the result :

A) playing football

playing hockey

playing football

B) playing football

playing hockey

playing hockey

C) Compilation error at line 12

D) Compilation error at line 22

E) Compilation error at line 33

F) Exception thrown at run time.


**********************************************

Q18 Given

1 public class test

2 {

3 static int i=0;

4 test()

5 {

6 i++;

7 }

8

9 public static void main(String args[])

10 {

11 test t=new test();

12 System.out.print(test.i);

13 test t1=new test();

14 System.out.print(test.i);

15 test2=new test();

16 System.out.print(test.i);

17 }

18 }

What is the result

A) 1 1 1

B) 0 0 0

C) 1 2 3

D) Code does not compile at line 14

E) Exception thrown at runtime


****************************************************



Q17) Given the following

1 class A

2 {

3 final int x=10;

4 protected final void message()

5 {

6 System.out.println("A");

7 }

8 protected void getValue()

9 {

10 System.out.println(x);

11 }

12 }

13 public class B extends A

14 {

15 public void message()

16 {

17 System.out.println("B");

18 }

19 public static void main(String args[])

20 {

21 A obj1=new A();

22 obj1.message();

23 B obj2=new B();

24 obj2.message();

25 obj1=obj2;

26 obj1.message();

27 obj1.getValue();

28 }

29 }


What is the result ?


A) A
B
B
10

B) A
B
A
10

C) Compilation fails at line 15

D) Compilation fails at line 25

E) Compilation fails at line 27
[ September 02, 2008: Message edited by: ElanKumaran Srinivasan ]
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Thank you for posting. However when you post a question here it is required to tell us where these questions are from. Did you write them on your own ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Also after telling us the source, please format the code using the code tags.

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
answer 1:
compiler error in line b1.details();
because details() method is not overriden in student class.
answer 2:
0 ,1 as in test(int x) constructor value of local variable is changing.

answer 19
play footba
play hockey
play hockey

because satic method play() can't be overriden.It's only hidding.

answer 18
compiler error in line test2=new test();
answer 17
comiler error in line 15. because final method can't be overriden
[ September 02, 2008: Message edited by: subhasish nag ]

Thanks,<br />Subhasish
ElanKumaran Srinivasan
Greenhorn

Joined: Sep 02, 2008
Posts: 3
Hi,

I wrote the questions myself and wanted to know if they are up to the standard.

Here's one more..

3 Given the following

1 class base

2 {

3 int x=0;

4 base()

5 {

5 x++;

6 }

7 base (int i)

8 {

9 --x;

10 }

11 }

12

13 public class child extends base

14 {

15 child()

16 {

17 ++x;

18 }

19 child(int x)

20 {

21 this();

22 x++;

23 }

24

25 public static void main(String args[])

26 {

27 child c=new child(10);

28 System.out.println("The value of x is "+c.x);

29 }

30 }





What is the result ?

A) The value of x is 12

B) The value of x is 11

C) The value of x is 13

D) The value of x is 1

E) The value of x is 2

F) None of the above is the correct answer
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Nice work Elan Kumaran.

However as an advice I would say that your questions are concentrated on a single type of gotcha...I am not saying that they are bad but they are a little repetitive.

But don't be discouraged. Your work is great. I am giving you a tip for improvement.....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

A tip which Bert already posted above: If you use code tags, the forum software will automatically format your Java code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ElanKumaran Srinivasan
Greenhorn

Joined: Sep 02, 2008
Posts: 3
Thanks Ankit, I will try to write better questions.

Jesper and Bates, thanks for the tip.Will try to make use of the code tags next time onwards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New java questions