• 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

package

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Where do get so many question? Do let me know please
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is E. The member class variable x is declared using the default access modifier in class Test1 so it is visible only inside the package test1. Trying to access it in class Test2(found in package test2) will generate a compilation error.

Shawnne
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the Class test1 is modified as follows:
package test1;
2 public class Test1 {
3 public static int x = 42;
4 }

then the answer would be:
choice: B.

Correct me if i am wrong..
regards
Balaji.S
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If the Class test1 is modified as follows:
package test1;
2 public class Test1 {
3 public static int x = 42;
4 }

then the answer would be:
choice: B.



Yes, public members are accessible in every class, every package
Regards
Naresh
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Since the program has

static x = 42;

I think there should be an compiler error at line 4. So I think the option E is correct.

Please correct me if I am wrong.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balaji Sampath:
If the Class test1 is modified as follows:
package test1;
2 public class Test1 {
3 public static int x = 42;
4 }

then the answer would be:
choice: B.

Correct me if i am wrong..
regards
Balaji.S



Hi Balaji,

even protected static int x = 42; will work,
and i think it is better to make memebers protected if possible than to make public ,otherwise u will be un necessarily opening doors to ur class,which is not desierd

Vivek
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u
 
reply
    Bookmark Topic Watch Topic
  • New Topic