aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Few Tricky Ques." Watch "Few Tricky Ques." New topic
Author

Few Tricky Ques.

Saurabh Malhotra
Greenhorn

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

int i=0;
do{
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???

if(x4)
SOPLn("Test 1");
else if(x9)
SOPLn("Test 2);
else
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
application
(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
Q10)
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)
{
super(s,a);
}

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

Saurabh
Valentin Crettaz
Gold Digger
Sheriff

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:


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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 once.so "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).
Q4)
a fully encapsulated classhas
1.private variables.
2.public methods to access theclass's properties.
Q5)final is correct.
Q6)
i did not completely understand the?.are u looking for "synchronized"?
Q7) 0.
Q10)this(s,a)/super(s,a)
Saurabh Malhotra
Greenhorn

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

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:
1

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.
2

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: http://aspose.com/file-tools
 
subject: Few Tricky Ques.
 
Similar Threads
Operators
Questions from Jxam
Confusion
a few doubts!!
Mock Exam Question.Please anybody help me.