• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam Qn.(What is the result)

 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. package test1;
2. public class Test1 {
3. static int x = 42;
4. }
1. package test2;
2. public class Test2 extends test1.Test1 {
3. public static void main(String[] args) {
4. System.out.println("x = " + x);
5. }
6. }
What is the result?
A. x = 0
B. x = 42
C. Compilation fails because of an error in line 2 of class Test2.
D. Compilation fails because of an error in line 3 of class Test1.
E. Compilation fails because of an error in line 4 of class Test2.
 
Poonam Thawani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its 42.
 
Chandrakanth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
42 is not the answer.

I am confused with these two...
When i run the program i am getting the answer as 'E', but the Mock Exam gives the answer as 'C'.
Just wanted to make sure, the answer is E or not

C. Compilation fails because of an error in line 2 of class Test2.
E. Compilation fails because of an error in line 4 of class Test2.
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the variable x has a default access
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi here, test1.Test1 class is not imported.
so line-2 gives compilation error.
 
Sowjanya Chowdary
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also getting answer as E.
Because, x is not declared public in Test1.java, so it can't be accessed outside the package.

Answer can't be C because, when using a class outside a package, we can either import that package or use fully-qualified-name.
Here in line 2 of Test2.java, we are using fully-qualified-name of Test1 class as "test1.Test1.java".

-Amrutha
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amrutha Chowdary:
Because, x is not declared public in Test1.java, so it can't be accessed outside the package.


-Amrutha


Hi ,
it will work with protected as well
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic