• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mock exam query

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Following are queries from mock exam on go4java.20msite.
Which one of the following statements will give you an Exception when
placed at line no.10?Select one correct answer.

1 class AA{}
2 class BB extends AA{}
3 class casting
4 {
5 public static void main(String arg[])
6 {
7 AA a=new AA();
8 BB b=new BB();
9 AA c=new BB();
10 //Here
11 }
11 }

1. b = (BB) a;
2. a = b ;
3. b = (BB) c;
4. c = b;

class Q20
2 {
3 public static void main(String arg[])
4 {
5 Q20 e=null;
6 System.out.println(e.equals(null));
7 Q20 e1;
8 System.out.println(e1.equals(null));
9 }
10 }

1. Error at line no. 6
2. Excepetion at line no. 6
3. Error at line no. 8
4. Excepetion at line no. 8

What will be the result of compiling and running the given program?
Select one correct answer.
1 class initialize
2 {
3 int y=0;
4 {
5 y=1;
6 }
7 initialize()
8 {
9 this(10);
10 y=3;
11 }
12 initialize(int s)
13 {
14 y=2;
15 }
16 public static void main(String arg[])
17 {
18 initialize i=new initialize();
19 System.out.println(i.y);
20 }
21 }


1. Compile time error
2 Program compiles correctly and prints 1 when executed.
3. Program compiles correctly and prints 2 when executed.
4. Program compiles correctly and prints 3 when executed.
5. Program compiles correctly and prints 0 when executed.

What is the answer?
Thankyou
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your second question the answer is option number 4
explanation:
first constructor initialize() is called and then this(10) call the constructor initialise(int) and then at last y=3 is executed.

So the output is 3
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ketki

Following is the explanation with answers -

Q1 -
1. b = (BB) a; is the answer.

explanation -
Here a super class reference is being assigned to child class reference (downcasting).
As per rules downcasting can be done only after Upcasting.
i.e. if b = (BB) a; is written after a = b; then no exception will be generated.

Q2 -
3. Error at line no. 8 is the answer.
explanation -
Here, 'e1' is not initialized yet and we are trying to call .equals(), thus its a compiler error.
Line# 6 is ok as 'e' is initialized to 'null'.

Q3 -
'4. Program compiles correctly and prints 3 when executed'. is the correct answer.
explanation -
First the initiazer block { y=1; } is called, then the parameterized constructor is called and finally non parameterized constructor is called and i.y becomes 3.

Hope this clarifies your doubts.
Cheers..
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mohit Jain:
Q2 -
3. Error at line no. 8 is the answer.
explanation -
Here, 'e1' is not initialized yet and we are trying to call .equals(), thus its a compiler error.
Line# 6 is ok as 'e' is initialized to 'null'.



Since we don't know the exact question, 2 and 3 could be the answer.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the second question,
class Q20 {
public static void main(String arg[]) {
Q20 e = null;
System.out.println(e.equals(null)); //6
Q20 e1;
System.out.println(e1.equals(null)); //8
}
}

@6 There will be run time NullPointerException
@8 It's compile tile error.

Sweta pillai
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic