File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help!!!" Watch "Help!!!" New topic


Sanjeet Karamchandani
Ranch Hand

Joined: Jul 29, 2000
Posts: 30
Hi friends,
I have a few examples that r strange and I can't understand them. Could anyone explain these to me. Most of the examples are from Jxam.
Q 1. File name :
class A {

private static void main(String[] args) {
System.out.println("In class A");

public class B extends A {

If this program is compiled, it compiles correctly and when run it displays "In class A". This is a bit strange. Could anyone explain this.

Q 2. Will this compile?
import java.awt.*;

class A extends java.applet.Applet {
Button one;
one = new Button("One");

public void init() {

public void paint(Graphics g) {

This program doesn't compile but its difficult for me to understand why?

Q.3 Given the following definition:
String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown.
1) if ((s != null) & (s.length()>0))
2) if ((s != null) && (s.length()>0))
3) if ((s != null) | (s.length()>0))
4) if ((s != null) | | (s.length()>0))
5) None of the above.
Correct ans : 1,2,3,4
But I think 2nd option will not cause an exception coz of the Short circuit operator as (s != null) will be false & (s.length() >0) will not be evaluated. so the correct ans should be 1,3,4.

Q.4 True or False.
It is valid to declare an inherited method as abstract.
Correct Ans : true
It is a bit strange and I couldn't understand the reason why its this way.

Q.5 True or False.
Only Frames can contain menu bars or pull-down menus.
Correct Ans : False
I feel its true. Actually I feel Window class and its subclasses (Frame & Dialog) can contain menubars and pull-down menus and so it could be false. This ex is from Jxam.

Q.6 You get this description of a class :
Employee is a person. For every employee, we keep a vector with the working hours,
an integer for the salary and a variable that can be true or false whether or not
the employee has a company car.
Indicate which of the following would be used to define the class members.
1) Vector
2) Employee
3) Object
4) boolean
5) int
Correct Answer
I feel it should be 1,4,5.
Amandeep Waraich
Ranch Hand

Joined: Jul 14, 2000
Posts: 56
Hi Sanjeet,
I also took the JXam mock exam just a while ago. I'm also doubtful about most of these qns. I didn't get the first one, altho it seems to be very interesting. The main method seems to be getting inherited while it shouldn't 'coz it's declared to be private. Well, regarding the question number 2, I think it won't give an error if you do the declaration and initialization of one in the same step. If you really want to initialize it in the second step, then you have to put brackets around it to indicate it as instance initializer (i don't remember whether this is the correct term). So, if you do :

one = new Button("One");

it should work.
Regarding the qn 3, I also answer 1, 2 and 4.
Regarding the qn 5, I don't think Window/Dialog can have menubar, 'coz they don't have any method for adding a menubar, similar to the method setMenuBar() of Frame class. So, I'm not sure why the answer is false.
Regarding the qn 6, I think there was some problem in the applet, 'coz if you look at the explanation for this qn, it says the same thing, that a vector, a boolean, and an int are required, but still it marks the question as wrong.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
ans 4 yes you can inherit a abstract method and declare it abstract so in that case its allowed so true is correct
Sanjeet Karamchandani
Ranch Hand

Joined: Jul 29, 2000
Posts: 30
Hi Aman,
Regarding Q.1, the Java language specification says that private members r not inherited so this program really seems strange.
Regarding Q.2, i know if i declare & initialize at the same time it works but i want to know which line of code is giving problem in its current form & why?. Could u tell me that.
Q.3, well 2nd option is not at all correct according to me because && short circuits the expression and so the s.length() > 0 will not be evaluated, isn't it?
Ranch Hand

Joined: May 26, 2002
Posts: 79
Qn 1
IMHO, private methods ARE inherited. The accessibility modifier only controls the visibility of the member. It does not get in the way of inheritance.
Any takers . . . ?

Joined: Jul 29, 2000
Posts: 4
Yah, declaring private controls the visibility of the method , not the execution.
Sanjeet Karamchandani
Ranch Hand

Joined: Jul 29, 2000
Posts: 30
I disagree with those who say that private members r inherited. And this is also stated in Java Language specification. Can anyone prove that private membes r inherited.
It is sorta covered in the JavaRanch Style Guide.
subject: Help!!!
Similar Threads
Eddie McNally's Jxam question...
JXam Qns