Tuesday, 31 October 2017

#9. SCJP/OCJP Exams Questions & Answers-



9. Given:
 class PingPong2 {
 synchronized void hit(long n) {
 for(int i = 1; i < 3; i++)
 System.out.print(n + "-" + i + " ");
 }
 }
 public class Tester implements Runnable {
 static PingPong2 pp2 = new PingPong2();
 public static void main(String[] args) {
 new Thread(new Tester()).start();
 new Thread(new Tester()).start();
 }
 public void run() {  pp2.hit(Thread.currentThread().getId()); }
 }
Which statement is true?
A. The output could be 5-1 6-1 6-2 5-2
B. The output could be 6-1 6-2 5-1 5-2
C. The output could be 6-1 5-2 6-2 5-1
D. The output could be 6-1 6-2 5-1 7-1

Correct Answer: B

#8. SCJP/OCJP Exams Questions & Answers-


8. What is the output if the main() method is run?
public class Starter extends Thread{
private int k=2
public Static void main(String[] args) throws Exception{
new starter().makeItDo();
}
public Starter(){
k=5;
start();
}
public void makeItDo() throws Exception{
join();
k=k-1;
System.out.println(k);
}
public void run(){
k*=2
}
}
A. 4
B. 5
C. 8
D. 9
E. Compilation fails.
F. An exception is thrown at runtime.
G. It is impossible to determine for certain.

Answer: D

#7. SCJP/OCJP Exams Questions & Answers-


7. Given:
 void waitForSignal() {
 Object obj = new Object();
 synchronized (Thread.currentThread()) {
 obj.wait();
 obj.notify();
 }
 }
 Which statement is true?
A. This code can throw an InterruptedException.
B. This code can throw an IllegalMonitorStateException.
C. This code can throw a TimeoutException after ten minutes.
D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally.
E. A call to notify() or notifyAll() from another thread might cause this method to complete normally.
F. This code does NOT compile unless "obj.wait()"is replaced with "((Thread)obj).wait()".
Answer: B

#6. SCJP/OCJP Exams Questions & Answers-


6. Which two statements are true? (Choose two.)
A. It is possible for more than two threads to deadlock at once.
B. The JVM implementation guarantees that multiple threads cannot enter into a deadlocked state.
C. Deadlocked threads release once their sleep() method's sleep duration has expired.
D. Deadlocking can occur only when the wait(), notify(), and notifyAll() methods are used incorrectly.
E. It is possible for a single-threaded application to deadlock if synchronized blocks are used incorrectly.
F. If a piece of code is capable of deadlocking, you cannot eliminate the possibility of deadlocking by inserting
invocations of Thread.yield().
Answer: A, F

#5. SCJP/OCJP Exams Questions & Answers-


5. Given:
12. public class Yippee2 {
13.
14. static public void main(String [] yahoo) {
15. for(int x = 1; x < yahoo.length; x++) {
16. System.out.print(yahoo[x] + " ");
17. }
18. }
19. }
and the command line invocation:
java Yippee2 a b c
What is the result?
A. a b
B. b c
C. a b c
D. Compilation fails.
E. An exception is thrown at runtime.

Correct Answer: B

#4. SCJP/OCJP Exams Questions & Answers-


4. class Test
{
public static void main(String[] args)
{
Integer i=254;
byte b=(byte)(int)i;
System.out.println(b);
}
}
what is the output of above program?

A. compile time error
B. Run time error
C. -2
D. 254
E. -1




Correct Answer: C

#3. SCJP/OCJP Exams Questions & Answers-Which method signature follows the JavaBeans naming standards for modifying the itemld instance variable?



3. A programmer is designing a class to encapsulate the information about an inventory item. A JavaBeans component is needed to do this. The Inventory ltem class has private instance variables to store the item information:
10. private int itemId;
11. private String name;
12. private String description;

Which method signature follows the JavaBeans naming standards for
modifying the itemld instance variable?

A. itemID(int itemId)
B. update(int itemId)
C. setItemId(int itemId)
D. mutateItemId(int itemId)
E. updateItemID(int itemId)



Correct Answer: C

Explanation:
JavaBeans  के standard naming  convention के according अगर हम private  instance variable को modify करना है तोह इस तरीके से लिखते है | 
setXXX( datatype XXX);
इसलिए ऑप्शन C सही है | 

#2. SCJP/OCJP Exams Questions & Answers-Which two statements are true?



2. Given:
35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;

Which two statements are true? (Choose two.)
A. Line 35 will not compile.
B. Line 36 will not compile.
C. Line 37 will not compile.
D. Line 38 will not compile.



Correct Answer. (A,D) 

Explanation. 
Java  के rule  के according , variable  name सिर्फ  alphabets , numerical digit, dollar sign "$", underscore "_" से  कर सकते है | option A  and option D इस rue  को follow नहीं कर रहे है इसलिए ये compile  नहीं होंगे |

#1. SCJP/OCJP Exams Question & Answer-Which two code fragments correctly create and initialize a Static Array of int element?



1. Which two code fragments correctly create and initialize a Static Array of int element?(Choose two.)
A. static final int[] a={100, 200};
B. static final int[] a;
   static { a=new int[2]; a[0]=100; a[1]=200; }
C. static final int[] a = new int[2]{100,200};
D. static final int[] a;
   static void init(){a = new int[3]; a[0]=100; a[1]=200;}  


Correct Answer. (A,B)
Explanation.
Syntax के अनुसार अगर देखे तोह option A सही है,क्योकि हम जानते है की Array का syntax  क्या होता है|
[Access modifiers] Datatype [] ref _var  = {Initialization}  or
[Access modifiers] Datatype [] ref_var =new Datatype[Size] इसलिए option A सही तरीके से create and initialize किया हुआ है |  
Option  B के फर्स्ट लाइन  में correctly integer array create किया हुआ है, और दूसरे लाइन में static block के अंदर integer array  के static data members को initialize किया हुआ है जो की सही है | जैसा की हम जानते है की static  block हमेशा JVM(java virtual machine) के द्वारा class  loading  के समय recognize and  initialize होती है | तो ये option  भी करेक्ट है |  
Option c में curly bracket के अंदर dimension expression define नहीं है , इसलिए ये option wrong  है | 
Option d  में कोई gurranty नहीं है की a variable initialize  हो सकता है | और हम init method  के अंदर  final  variable को initialize नहीं कर  सकते है |

आपलोगो अगर ये पोस्ट पसंद आया तो , please comment और share करे | 
Thanks !!!!

#45 to #51 SCJP/OCJP Exams Questions & Answers-

45. Given: 1. public class A { 2. public void doit() { 3. } 4. public String doit() { 5. return "a"; 6. } 7. ...