AmbitionBox
Discover Best Places to work in India
Discover best places to work
Compare & find best workplace
Bring your workplace to life
Highlight your company's perks
Read reviews for 6L+ companies
Rate your former or current company
Discover salaries for 8L+ companies
Calculate your take home salary
Check your market value
Help other jobseekers
Read interviews for 90K+ companies
Interviews questions for 1K+ colleges
Contribute your interview questions
Java Programming
Consider the following code:
1. public class ThreadOrder {
2. static int count=0;
3. public static void main(String[] args) {
4. Counter ct = new Counter();
5. Tracker trk1 = new Tracker(ct, "thread one");
6. Tracker trk2 = new Tracker(ct, "thread two");
7. trk1.start();
8. trk2.start();
9. }
10.}
11. class Tracker extends Thread {
12. Counter ct;
13. String message;
14. Tracker(Counter ct, String msg) {
15. this.ct = ct;
16. message = msg;
17. }
18. public void run() {
19. System.out.println(message);
20. }
21. }
22. class Counter {
23. private int count=0;
24. public int nextCounter() {
25. synchronized(this) {
26. count++;
27. return count;
28. }
29. }
30. }
What would be the output of this code fragment?
A. thread 1
thread 2
B. thread 2
thread 1
C. Sometimes A, sometimes B
D. Runtime exception on line 8
Which of the following is a method of the Object class?
A. yield ( )
B. sleep (..)
C. run ( )
D. wait ( )
Which of the following methods guarantee putting a thread out of the running state?
A. wait ( )
B. yield ( )
C. kill ( )
D. notify ( )
Which of the following is an invalid Thread constructor?
A. Thread ( )
B. Thread ( into millisec)
C. Thread ( Runnable r)
D. Thread ( Runnable R, String name)
Consider the following code fragment:
Integer w1 = new Integer(2);
Integer w2 = new Integer(2);
if(w1 == w2)
{
System.out.println("w1 is equal to w2!");
}
else
System.out.println("w1 is not equal to w2!");
What is the output of this code?
A. Compiler error
B. Runtime error
C. w1 is equal to w2!
D. w1 is not equal to w2!
1. ArrayList list = new ArrayList();
2. list.add(new ObjectOne());
3. list.add(new ObjectOne());
4. list.add(new ObjectOne());
5. Collections.sort(list);
6. class ObjectOne {
7. private int x = 0;
8. private int y = 0;
What is the output of this code fragment?
A. Compiler error at line 5
B. Runtime error at line 5
C. Compiler error at line 3
D. Runtime error at line 3
Which of the following is an illegal line of code?
A. HashMap hmap = new HashMap();
B. ArrayList list = new ArrayList();
C. List list2 = new ArrayList ();
D. All of the above
Which of the following collections can you use to store key-value pairs and is thread safe?
A. HashTable
B. HashMap
C. TreeMap
D. Vector
1. import java.io.*;
2. class MyClass{
3. public static void main(String[] args) throws IOException {
4. NumberStore ns = new NumberStore();
5. System.out.println("The number: " + ns.getInteger());
6. ns.setInteger(10);
7. System.out.println("The number: " + ns.getInteger());
8. }
10. class NumberStore {
11. int i =5;
12. public void setInteger(Integer x){
13. System.out.println("The number passed in is: " + x);
14. i = x;
15. }
16. public int getInteger( ){
17. return i;
18. }
19.}
What is the output?
A. Compiler error at line 6
B. Execution error at line 13
D. The number:5
The number passed in is:10
The number: 10
2. list.add(new Integer(1));
3. list.add(new Integer(2));
4. list.add(new Integer(3));
5. Iterator itr = list.iterator();
6. for(Integer wij:list){
7. System.out.println("number: " + wij);
What is the output from this code fragment?
A. number: 1
number: 2
number: 3
B. Compiler error at line 5
C. Compiler error at line 6
D. Runtime error
Join India’s largest community to research company culture
Are you a student or working professional?
Student/Never worked
I am a student/I have never worked
Working Professional
I am working/I have worked before
What are your preferred job locations?
Popular Cities
Other Cities
Follow your preferred designations/job profiles
Suggestions based on your job profile
vs
Similar Companies