This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Few Tricky Ques. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Few Tricky Ques." Watch "Few Tricky Ques." New topic

Few Tricky Ques.

Saurabh Malhotra

Joined: Jul 07, 2002
Posts: 6
Please help me out in finding the solutions of the following questions.
Q1).)Go throw the following code.

int i=0;
System.out.pintln("value of i is"+ i);
while (i--<0);
System.out.pintln("the end");

Here i-- will be -1,and -1 is less than 0 means while is true so do loop shouls continue printing.
But the output is : value of i is 0
the end.
Please explain why???

Q2)String s=null.
Which of the following codes will throw NullPointerException. and WHY
a)s!=null && s.length()=0;
b)s!=null & s.length()=0;
c)s!=null ll s.length()=0;
d)s!=null l s.length()=0;
Q3)what values of x of the follwoing code display "Test 2" and WHY???
Can u explain the logic also???

SOPLn("Test 1");
else if(x9)
SOPLn("Test 2);
SOPLn("Test 3");
a) x is valued between 0-4.
b) x is valued between 4-9.
c) x is valued more than 10
d) None
Q4)for fully ecapsulation of a class, which are true?
a)make all variables private
b) make all methods private.
c)make variables nd methods private.
d) make methods as protected,default, privat and variables as private.
Q5)what keyword is allowd for member variables to be initialed only once???
(is it final,)
Q6)what keywod would be used to get object lock of this?
Q7)int i=new int[10];
what is the value of i[5]
Q8)Can we stop a thread indefinately if we wish to(True/False)
If True ,how.
Q9)An application consists of a set of user interfaces & the various operations are to be performed based on the various user invoked events. Which of the following are true for the above
(i) Extending Adapter classes is not suitable
(ii) Some listener interface must be implemented
(iii) If a particular event listener is implemented only the methods of interest to us need to be implemented
(iv) Multiple event listeners can be added to a single component
(v) The order in which the events are processed is the order in which the listeners are added
class A
String str;
int x;
int z;

public A(String s , int a)
str = s ;
x = a ;

public A(int a,String s)

class B extends A
public B(String s, int a)

public B(String s, int a, int c)
z = c;
What code can be inserted at //P so that it fully implements the existing code to initialise the values of x & str.

Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Could you please tell me from which source you got those questions?
Also it would help if you post each question in separate topics :roll:

[Blog] [Blogroll] [My Reviews] My Linked In
pree sree
Ranch Hand

Joined: Jul 27, 2002
Posts: 34
some answers are given.
Q1) the do loop executes atleast "the value of i is=0" comes first and then it checks for i--<0 and hence comes out of the loop.
Q3)i think it will not compile since the if needs a boolean expression ie, if(boolean expression).
a fully encapsulated classhas
1.private variables.
2.public methods to access theclass's properties.
Q5)final is correct.
i did not completely understand the?.are u looking for "synchronized"?
Q7) 0.
Saurabh Malhotra

Joined: Jul 07, 2002
Posts: 6
I just found these questions from mock exams
Jonathas Carrijo

Joined: Jul 25, 2002
Posts: 25
Question 1
The expression (i-- < 0) first returns the value resulting from i<0 then decrements the variable i! Thus, i is 0 when the first comparison is made and it returns false!
Question 2
What you gotta know:
<exp1> && <exp2> : if <exp1> is false, <exp2> is not evaluated, because despite of its value, the whole value is already false.
<exp1> & <exp2> : both expressions are always evaluated and a bitwise AND is made.
<exp1> || <exp2> : if <exp1> is true, <exp2> is not evaluated, because the condition of at least one value is true is already fulfilled.
<exp1> | <exp2> : both exps always evaluated...
Thus, b and d will shurely throw NullPointerException.
- a will not, because the result from the first expression is false, and the second is not evaluated.
- c wouldn't throw it if the first exp was true, and since it's not, the second exp is evaluated and exception is thrown.
Question 3: Think you mistyped it...
But, trying to guess (if the missing operators are all <, and supposing that between doesn't mean including)
-> b (supposing 9 is not included)
Question 4: I think a and d would do it... :roll:
Question 5: yup
Question 7: it depends on wether you are inside a method or not. Variables are initialized with zero like values if they're member variables, i.e., outside methods. This is true for array values too.
Question 9:
-(i): false. Of course it is! They're useful when you need to implement just one of a interface's methods. Especially if the interface provides several methods to implement.
-(ii): false. Not necessarily, if the events invoked are those already treaded by Java API itself.
-(iii): false. Not true, except if you use Adapter classes.
-(iv): true
-[b](v)[b]: false. Cannot be determined.
[ September 03, 2002: Message edited by: Jonathas Carrijo ]
[ September 03, 2002: Message edited by: Jonathas Carrijo ]
[ September 03, 2002: Message edited by: Jonathas Carrijo ]

Java has reached Brazil =D
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hello there:

The control will enter the loop without any checking and it will print "value of i is 0".
At the end of first loop, i-- will give the current value to the expression and then it will be decreamented. Therefore, (0<0) will evaluate to false and loop will be exited, and "the end" will be printed.

String s=null.
Which of the following codes will throw NullPointerException. and WHY
a)s!=null && s.length()=0;
b)s!=null & s.length()=0;
c)s!=null ll s.length()=0;
d)s!=null l s.length()=0;

I think the code is not properley written. I revised it as follows:

LINE 1: The && operator will first evalute (s!= null), which will be fales. Therefore, rigth side of && will not be evaluated.
LINE 2: The & operator will evaluate its both sides. The right side will give the runtime exception.
LINE 3: The || operator will evaluate its left side first which is fales. As it is false, the rigth side will have to evaluated, which will result in runtime exception. If left side would evaluated to true, the right side will NOT be evaluated and hence no runtime exception.
LINE 4: The | operator will evaluate its both sides no matter what. Therefore, it will give runtime exception.
[ September 03, 2002: Message edited by: Barkat Mardhani ]
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
For question 10
this(s,a) whould be an option.
This calls the default constructor which leads to the call of super(s,a) and thus initializing str and a

Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Sabarish Sasidharan
Ranch Hand

Joined: Aug 29, 2002
Posts: 73
Q8)Can we stop a thread indefinately if we wish to(True/False)
If True ,how.
The deprecated method suspend() can stop a thread indefinitely. Hence answer should be True

Sab<br /> <br />Perfection does not come from belief or faith. Talk does not count for anything. Parrots can do that. Perfection comes through selfless work.<br />Swami Vivekananda
I agree. Here's the link:
subject: Few Tricky Ques.
It's not a secret anymore!