• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Few Tricky Ques.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just found these questions from mock exams
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic