Showing posts with label core java question. Show all posts
Showing posts with label core java question. Show all posts

Friday, December 23, 2016

JAVA Interview Question

Q. 41
11. class Alpha {
12. public void foo() { System.out.print(“Afoo”); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print(“Bfoo”); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();  20. b.foo();
21. }
22. }
What is the result?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.

Q. 42 Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized(Thread.currentThreat()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A. This code may throw an interruptedException.
B. This code may throw an illegalstateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless “obj.wait()” is replaced with “((Thread) obj).wait()”.
E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.
F. A call to notify() or notifyAll() from another thread may cause this method to complete normally.

Q, 43 Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11. }
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable “x” are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized (new Object()) { } block would make the class thread-safe.

Q. 44 Given:
11. public void testIfA() {
12. if (testlfB(“True”)) {
13. System.out.println(“True”);
14. } else {
15. System.out.println(“Not True”);
16. }
17. }
18. public Boolean testlfB(String str) {
19. return Boolean.valueof(str);
20. }
What is the result when method testlfA is invoked?
A. True
B. Not True
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12
E. Compilation fails because of an error at line 13.

Q.45 when comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two?
A. closing the stream.
B. flushing the stream
C. writing to the Stream
D. marking a location in the stream
E. writing a line separator to the stream

46. The calendar for the year 2007 will be the same for the year.
A. 2014 B. 2016 c. 2017                  D. 2018

47. A man standing at a point P is watching the top of a tower, which makes an angle of elevation of 30 degree with the man’s eye. The man walks some distance towards the tower to watch its top and the angle of the elevation becomes 60 degree. What is the distance between the base of the tower and the point P?
A. 43 units           B. 8 units             C. 12 units           D. Data inadequate.

48. Two ships are sailing in the sea on the two sides of a lighthouse. The angle of elevation of the top of the lighthouse is observed from the ships are 30 degree and 45 degree respectively. If the lighthouse is 100 m high, the distance between the two ships is:
A. 173 m               B. 200 m               C. 273 m               D. 300 m

49. A and B take part in 100 m race. A runs at 5 kmph. A gives B a start of 8 m and still beats him by 8 seconds. The speed of B is:
A. 5.15 kmph      B. 4.14 kmph      C. 4.25 kmph      D. 4.4 kmph

50. In 100 m race, A covers the distance in 36 seconds and B in 45 seconds. In this race A beats B by:

A. 20 m                 B. 25m                  C. 22.5m               D. 9 m

Thursday, December 22, 2016

JAVA Interview Question 31-40

Q: 31 Given:
15. public class Yippee {
16. public static void main(String[] args) {
17. for (int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + “ “);
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4

What is the result?
A. No output is produced.
1 2 3
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3 4
D. An exception is thrown at runtime.
1 2 3
E. An exception is thrown at runtime.
2 3 4
F. An exception is thrown at runtime.
1 2 3 4

Q. 32 Given:

10. class Nav{
11. public enum Direction {NORTH, SOUTH, EAST, WEST}
12. }
13. public class Sprite {
14. // insert code here
15. }

Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav. Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;

Q: 33 Given:
11. public class Ball{
12. public enum Color {RED, GREEN, BLUE};
13. public void fool(){
14. // insert code here
15. { System.out.println(c);}
16. }
17. }
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?
A. for(Color c : Color.values() }
B. for(Color c = RED; c <= BLUE; c++)
C. for(Color c; c.hasNext(); c.next() )
D. for(Color c = Color[0]; c <= Color[2]; c++)
E. for(Color C = Color.RED; c <= Color.BLUE; c++)

Q. 34 Given:
3. public class Spock{
4. public static void main(String[] args){
5. Long tail = 2000L
6. Long distance = 1999L
7. Long story = 1000L
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print(“1”);
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print(“2”) ;
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

Q. 35 Given:
25. int x = 12;
26. while (x<10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. 0
B. 10
C. 12
D. Line 29 will never be reached.

Q. 36 Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError ();
13. System.out.print(“test”);
14. }
15. public static void main(String[] args) {
16. try(test); }
17. catch (Exception ex} {System.out.print(“exception”); }
18. System.out.print(“end”);
19. }
What is the result?
A. end
B. Compilation fails.
C. exception end.
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.

Q. 37 Given:
33. try {
34. // some cade here
35. } catch (NullPointerException e1) {
36. System.out.print(“a”);
37. } catch (RuntimeException e2) {
38. System.out.print(“b”);
39. } finally {
40. System.out.print(“c”);
41. }
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. ab
D. ac
E. bc
F. abc

Q. 38
Click the Exhibit Button.
1. public class Test {
2.
3. public static void main( String[] args) {
4. Boolean assert = true;
5. if(assert) {
6. System.out.println(“assert is true”);
7. }
8. }
9.
10. }
Given:
Javac –source 1.3 Test.java

What is the result?
A. Compilation fails.
B. Compilatin Succeeds with errors.
C. Compilation succeeds with warnings.
D. Compilation succeeds without warnings or errors.

Q. 39 Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; I<10; i++) {
14. int value = i * ((int) Math.random());
15. integer intObj = new Integer(Value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.

Q. 40 Given:
1. class SuperClass {
2. public A getA(){
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass {
7. public B getA() {
8. return new B();
9. }
10. }
Which statement is true?
A. Compilation will succeed if A extends B.
B. Compilation will succeed if B extends A.
C. Compilation will always fail because of an error in line 7
D. Compilation will always fail because of an error in line 8


Monday, December 19, 2016

Interview Question 21 - 30

Interview Question 21 - 30

21. A bank offers 5% compound interest calculated on half-yearly basis. A customer deposits Rs. 1600 each on 1st January and 1st July of a year. At the end of the year, the amount he would have gained by way of interest is:
A. Rs. 120             B. Rs. 121             C. Rs. 122             D. Rs. 123

22. The difference between simple and compound interests compounded annually on a certain sum of money for 2 years at 4% per annum is Re 1. The sum (in Rs.) is –
A. 625                    B. 630                    C. 640                    D. 650

23. A motorboat, whose speed in 15km/hr In still water goes 30 km downstream and comes back in a total of 4 hours 30 minutes. The speed of the stream (in km/hr) is:
 A. 4                       B. 5                         C. 6                         D. 10

24. A man can row three-quarters of a kilometre against the stream in 11 minutes and down the stream in 7 minutes. The speed (in km/hr) of the man in still water is:
A. 2                        B. 3                         C. 4                         D. 5

25. The least perfect square, which is divisible by each of 21, 36 and 66 is:
A.21344                B. 214344             C. 214434             D. 231444

26. A shop keeper sells milk which contains 5% water. What quantity of pure milk should be added to 2 litres milk (containing 5% water) so that proportion of water becomes 4%?
A. 100 ml              B. 250 ml              C. 400 ml              D. 350 ml              E. None of these

27. A and B started a  business in partnership investing Rs 20,000 and Rs 15,000 respectively. After six months C joined them with Rs 20,000. What will be B’s share in the total profit of Rs 25,000 earned at the end of two years from the starting of the business?
A.  Rs 9,000         B. Rs 10,000        C. Rs 7,500           D. Rs 9,500          E. None of these

28. What approximate value should come in place of the question mark (?)  in the following equation?
158.25 x 4.6 + 21% of 847 + ? = 950.93
A. 35                      B. 40                      C. 25                      D. 50                      E. 45

29. The average of the first and the second of three numbers is 15 more than the average of the second and the third of these numbers. What is the difference between the first and the third of these three numbers?
A. 15                      B. 45                      C. 60                      D. Data inadequate         E. None of These

30. A man starts going for morning walk every day. The distance walked by him on the first day was 2 km. Every day he walks half the distance walked on the previous day. What can be the maximum total distance walked by him in his lifetime?
A. 4 km                 B. 20 km               C. 8 km                 D. Data inadequate         E. None of these

Sunday, December 18, 2016

JAVA Interview Question 10 - 20

JAVA Interview Question  10 - 20

Q. 11
11. class Alpha {
12. public void foo() { System.out.print(“Afoo”); }
13. }
14. public class Beta extends Alpha {
15. public void foo() { System.out.print(“Bfoo”); }
16. public static void main(String[] args) {
17. Alpha a = new Beta();
18. Beta b = (Beta)a;
19. a.foo();  20. b.foo();
21. }
22. }
What is the result?
A. Afoo Afoo
B. Afoo Bfoo
C. Bfoo Afoo
D. Bfoo Bfoo
E. Compilation fails.
F. An exception is thrown at runtime.

Q. 12 Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized(Thread.currentThreat()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A. This code may throw an interruptedException.
B. This code may throw an illegalstateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless “obj.wait()” is replaced with “((Thread) obj).wait()”.
E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.
F. A call to notify() or notifyAll() from another thread may cause this method to complete normally.

Q, 13 Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11. }
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable “x” are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized (new Object()) { } block would make the class thread-safe.

Q. 14 Given:
11. public void testIfA() {
12. if (testlfB(“True”)) {
13. System.out.println(“True”);
14. } else {
15. System.out.println(“Not True”);
16. }
17. }
18. public Boolean testlfB(String str) {
19. return Boolean.valueof(str);
20. }
What is the result when method testlfA is invoked?
A. True
B. Not True
C. An exception is thrown at runtime.
D. Compilation fails because of an error at line 12
E. Compilation fails because of an error at line 13.

Q. 15 when comparing java.io.BufferedWriter to java.io.FileWriter, which capability exists as a method in only one of the two?
A. closing the stream.
B. flushing the stream
C. writing to the Stream
D. marking a location in the stream
E. writing a line separator to the stream
     
16. The calendar for the year 2007 will be the same for the year.
A. 2014                 B. 2016                       c. 2017                  D. 2018

17. A man standing at a point P is watching the top of a tower, which makes an angle of elevation of 30 degree with the man’s eye. The man walks some distance towards the tower to watch its top and the angle of the elevation becomes 60 degree. What is the distance between the base of the tower and the point P?
A. 43 units           B. 8 units              C. 12 units           D. Data inadequate.

18. Two ships are sailing in the sea on the two sides of a lighthouse. The angle of elevation of the top of the lighthouse is observed from the ships are 30 degree and 45 degree respectively. If the lighthouse is 100 m high, the distance between the two ships is:
A. 173 m               B. 200 m               C. 273 m               D. 300 m

19. A and B take part in 100 m race. A runs at 5 kmph. A gives B a start of 8 m and still beats him by 8 seconds. The speed of B is:
A. 5.15 kmph      B. 4.14 kmph      C. 4.25 kmph      D. 4.4 kmph

20. In 100 m race, A covers the distance in 36 seconds and B in 45 seconds. In this race A beats B by:
A. 20 m                 B. 25m                  C. 22.5m               D. 9 m

Saturday, December 17, 2016

JAVA Interview Question 0-10

JAVA Interview Question 0-10

Q: 01 Given:
15. public class Yippee {
16. public static void main(String[] args) {
17. for (int x = 1; x < args.length; x++) {
18. System.out.print(args[x] + “ “);
19. }
20. }
21. }
and two separate command line invocations:
java Yippee
java Yippee 1 2 3 4

What is the result?
A. No output is produced.
1 2 3
B. No output is produced.
2 3 4
C. No output is produced.
1 2 3 4
D. An exception is thrown at runtime.
1 2 3
E. An exception is thrown at runtime.
2 3 4
F. An exception is thrown at runtime.
1 2 3 4

Q. 02 Given:

10. class Nav{
11. public enum Direction {NORTH, SOUTH, EAST, WEST}
12. }
13. public class Sprite {
14. // insert code here
15. }

Which code, inserted at line 14, allows the Sprite class to compile?
A. Direction d = NORTH;
B. Nav. Direction d = NORTH;
C. Direction d = Direction.NORTH;
D. Nav.Direction d = Nav.Direction.NORTH;

Q: 3 Given:
11. public class Ball{
12. public enum Color {RED, GREEN, BLUE};
13. public void fool(){
14. // insert code here
15. { System.out.println(c);}
16. }
17. }
Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?
A. for(Color c : Color.values() }
B. for(Color c = RED; c <= BLUE; c++)
C. for(Color c; c.hasNext(); c.next() )
D. for(Color c = Color[0]; c <= Color[2]; c++)
E. for(Color C = Color.RED; c <= Color.BLUE; c++)

Q. 4 Given:
3. public class Spock{
4. public static void main(String[] args){
5. Long tail = 2000L
6. Long distance = 1999L
7. Long story = 1000L
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print(“1”);
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print(“2”) ;
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.

Q. 5 Given:
25. int x = 12;
26. while (x<10) {
27. x--;
28. }
29. System.out.print(x);
What is the result?
A. 0
B. 10
C. 12
D. Line 29 will never be reached.

Q. 6 Given:
11. static void test() throws Error {
12. if (true) throw new AssertionError ();
13. System.out.print(“test”);
14. }
15. public static void main(String[] args) {
16. try(test); }
17. catch (Exception ex} {System.out.print(“exception”); }
18. System.out.print(“end”);
19. }
What is the result?
A. end
B. Compilation fails.
C. exception end.
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.

Q. 7 Given:
33. try {
34. // some cade here
35. } catch (NullPointerException e1) {
36. System.out.print(“a”);
37. } catch (RuntimeException e2) {
38. System.out.print(“b”);
39. } finally {
40. System.out.print(“c”);
41. }
What is the result if a NullPointerException occurs on line 34?
A. c
B. a
C. ab
D. ac
E. bc
F. abc

Q. 8
Click the Exhibit Button.
1. public class Test {
2.
3. public static void main( String[] args) {
4. Boolean assert = true;
5. if(assert) {
6. System.out.println(“assert is true”);
7. }
8. }
9.
10. }
Given:
Javac –source 1.3 Test.java

What is the result?
A. Compilation fails.
B. Compilatin Succeeds with errors.
C. Compilation succeeds with warnings.
D. Compilation succeeds without warnings or errors.

Q. 9 Given:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; I<10; i++) {
14. int value = i * ((int) Math.random());
15. integer intObj = new Integer(Value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }

Which line of code marks the earliest point that an object referenced by intObj becomes a candidate for garbage collection?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.

Q. 10 Given:
1. class SuperClass {
2. public A getA(){
3. return new A();
4. }
5. }
6. class SubClass extends SuperClass {
7. public B getA() {
8. return new B();
9. }
10. }
Which statement is true?
A. Compilation will succeed if A extends B.
B. Compilation will succeed if B extends A.
C. Compilation will always fail because of an error in line 7
D. Compilation will always fail because of an error in line 8

Write the Answers in Comments. i will  post correct answer with the name of person. Help me to get Answer.