COREJAVA

COREJAVA Objective Questions And Answers
More interview questions and answers

4) The Java Program is enclosed in a class definition? a) True b) False

a) True

3) The Java source code can be created in a Notepad editor? a) True b) False

a) True

2) On successful compilation a file with the class extension is created? a) True b) False

true

Given: 1. public class Test { 2. public static void main(String args[]) { 3. class Foo { 4. public int i = 3; 5. } 6. Object o = (Object)new Foo(); 7. Foo foo = (Foo)o; 8. System.out.println("i = " + foo.i); 9. } 10. } What is the result? A. i = 3 B. Compilation fails. C. A ClassCastException is thrown at line 6. D. A ClassCastException is thrown at line 7.

A ClassCastException is thrown at line 6

Which two cause a compiler error? (Choose two) A. float[] = new float(3); B. float f2[] = new float[]; C. float[] f1 = new float[3]; D. float f3[] = new float[3]; E. float f5[] = { 1.0f, 2.0f, 2.0f }; F. float f4[] = new float[] { 1.0f. 2.0f. 3.0f};

A. float[] = new float(3);
B. float f2[] = new float[];

1. class Test { 2. private Demo d; 3. void start() { 4. d = new Demo(); 5. this.takeDemo(d); 6. } 7. 8. void takeDemo(Demo demo) { 9. demo = null; 10. demo = new Demo(); 11. } 12. } When is the Demo object, created on line 3, eligible for garbage collection? A. After line 5. B. After line 9. C. After the start() method completes. D. When the takeDemo() method completes. E. When the instance running this code is made eligible for garbage collection.

E. When the instance running this code is made eligible for garbage collection. 

11. int i =1,j =10; 12. do { 13. if(i++> --j) { 14. continue; 15. } 16. } while (i <5); 17. System.out.println("i = " +i+ "and j = "+j); What is the result? A. i = 6 and j = 5 B. i = 5 and j = 5 C. i = 6 and j = 5 D. i = 5 and j = 6 E. i = 6 and j = 6

D. i = 5 and j = 6

Which statement is true about assertion in the Java programming language? A. Assertion expressions should not contain side effects. B. Assertion expression values can be any primitive type. C. Assertion should be used for enforcing preconditions on public methods. D. An AssertionError thrown as a result of a failed assertion should always be handled by the enclosing method.

A. Assertion expressions should not contain side effects.

1. package foo; 2. 3. import java.util.Vector; 4. 5. private class MyVector extends Vector { 6. int i = 1; 7. public MyVector() { 8. i = 2; 9. } 10. } 11. 12. public class MyNewVector extends MyVector { 13. public MyNewVector() { 14. i = 4; 15. } 16. public static void main(String args[]) { 17. MyVector v = new MyNewVector(); 18. } 19. } What is the result? A. Compilation succeeds. B. Compilation fails because of an error at line 5. C. Compilation fails because of an error at line 6. D. Compilation fails because of an error at line 14. E. Compilation fails because of an error at line 17.

B. Compilation fails because of an error at line 

1. class R4R{ 2. R4R(int i) { } 3. } 4. class TestSub extends R4R{ } 5. class TestAll { 6. public static void main (String [] args) { 7. new TestSub(); 8. } 9. } Which is true? A. Compilation fails. B. The code runs without exception. C. An exception is thrown at line 7. D. An exception is thrown at line 2

A. Compilation fails.

1. public class SwitchTest { 2. public static void main(String[] args) { 3. System.out.println("value = " + switchIt(4)); 4. } 5. public static int switchIt(int x) { 6. int j = 1; 7. switch (x) { 8. case 1: j++; 9. case 2: j++; 10. case 3: j++; 11. case 4: j++; 12. case 5: j++; 13. default: j++; 14. } 15. return j + x; 16. } 17. } What is the result? A. value = 3 B. value = 4 C. value = 5 D. value = 6 E. value = 7 F. value = 8

E. value = 7

int i = 0; for (; i <4; i += 2) { System.out.print(i +""); } System.out.println(i); What is the result? A. 0 2 4 B. 0 2 4 5 C. 0 1 2 3 4 D. Compilation fails. E. An exception is thrown at runtime.

A. 0 2 4

Which three form part of correct array declarations? (Choose three) A. public int a [] B. static int [] a C. public [] int a D. private int a [3] E. private int [3] a [] F. public final int [] a

A, B, F

1.public class Foo { 2. public static void main(String[] args) { 3. try { 4. return; 5. } finally { 6. System.out.println( "Finally" ); 7. } 8. } 9. } What is the result? A. Finally B. Compilation fails. C. The code runs with no output. D. An exception is thrown at runtime.

A. Finally

ClassOne.java: 1. package r4r.co.in.pkg1; 2. public class ClassOne { 3. private char var = "a"; 4. char getVar() { return var; } 5. } ClassTest.java: 1. package r4r.co.in.pkg2; 2. import r4r.co.in.pkg1.ClassOne; 3. public class ClassTest extends ClassOne { 4. public static void main(String[] args) { 5. char a = new ClassOne().getVar(); 6. char b = new ClassTest().getVar(); 7. } 8. } What is the result? A. Compilation fails. B. Compilation succeeds and no exceptions are thrown. C. An exception is thrown at line 5 in ClassTest.java. D. An exception is thrown at line 6 in ClassTest.java.

A. Compilation fails

1. public class Alpha1 { 2. public static void main( String[] args ) { 3. boolean flag; int i=0; 5. do { 6. flag = false; 7. System.out.println( i++ ); 8. flag = i < 10; 9. continue; 10. } while ( (flag)? true:false ); 11. } 12. } What is the result? A. 000000000 B. 0123456789 C. Compilation fails. D. The code runs with no output. E. The code enters an infinite loop. F. An exception is thrown at runtime.

B. 0123456789

1. package foo; 2. 3. import java.util.Vector; 4. 5. protected class MyVector Vector { 6. init i = 1; 7. public MyVector() { 8. i = 2; 9. } 10. } 11. 12. public class MyNewVector extends MyVector { 13. public MyNewVector() { 14. i = 4; 15. } 16. public static void main(String args[]) { 17. MyVector v = new MyNewVector(); 18. } 19. } What is the result? A. Compilation succeeds. B. Compilation fails because of an error at line 5. C. Compilation fails because of an error at line 6. D. Compilation fails because of an error at line 14. E. Compilation fails because of an error at line 17

B.Compilation fails because of an error at line 5.

1. class Super { 2. public Integer getLenght() { return new Integer(4); } 3. } 4. 5. public class Sub extends Super { 6. public Long GetLenght() { return new Long(5); } 7. 8. public static void main(String[] args) { 9. Super sooper = new Super(); 10. Sub sub = new Sub(); 11. System.out.println( 12. sooper.getLenght().toString() + "," + 13. sub.getLenght().toString() ); 14. } 15. } What is the output? A. 4,4 B. 4,5 C. 5,4 D. 5,5 E. Compilation fails.

A. 4,4

1. class Base { 2. Base() { System.out.print(“Base”); } 3. } 4. public class Alpha extends Base { 5. public static void main( String[] args ) { 6. new Alpha(); 7. new Base(); 8. } 9. } What is the result? A. Base B. BaseBase C. Compilation fails. D. The code runs with no output. E. An exception is thrown at runtime.

B. BaseBase

11. int i = 1,j = -1; 12. switch (i) { 13. case 0, 1:j = 1; 14. case 2: j = 2; 15. default; j = 0; 16. } 17. System.out.println(“j=”+j); What is the result? A. j = -1 B. j = 0 C. j = 1 D. j = 2 E. Compilation fails.

E.Compilation fails

1. public class X { 2. public static void main(String [] args) { 3. try { 4. badMethod(); 5. System.out.print(“A”); 6. } 7. catch (Exception ex) { 8. System.out.print(“B”); 9. } 10. finally { 11. System.out.print(“C”); 12. } 13. System.out.print(“D”); 14. } 15. public static void badMethod() {} 17. } What is the result? A. AC B. BD C. ACD D. ABCD E. Compilation fails.

C.ACD

Which two are valid declarations within an interface definition? (Choose two) A. void methoda(); B. public double methoda(); C. public final double methoda(); D. static void methoda(double d1); E. protected void methoda(double d1);

A. void methoda();
B. public double methoda();

Which two allow the class Thing to be instantiated using new Thing()? (Choose two) A. public class Thing { } B. public class Thing { public Thing() {} } C. public class Thing { public Thing(void) {} } D. public class Thing { public Thing(String s) {} } E. public class Thing { public void Thing() {} public Thing(String s) {} }

A. public class Thing {
}
B. public class Thing {
public Thing() {}
}

Float f = new Float(“12”); switch (f) { case 12: System.out.println(“Twelve”); case 0: System.out.println(“Zero”); default: System.out.println(“Default”); } What is the result? A. Zero B. Twelve C. Default D. Twelve Zero Default E. Compilation fails.

E. Compilation fails.

public class X { public static void main(String [] args) { try { badMethod(); System.out.print(“A”); } catch (Exception ex) { System.out.print(“B”); } finally { System.out.print(“C”); } System.out.print(“D”); } public static void badMethod() { throw new RuntimeException(); } } What is the result? A. AB B. BC C. ABC D. BCD E. Compilation fails.

D. BCD

1. class TestA { 2. TestB b; 3. TestA() { 4. b = new TestB(this); 5. } 6. } 7. class TestB { 8. TestA a; 9. TestB(TestA a) { 10. this.a = a; 11. } 12. } 13. class TestAll { 14. public static void main (String args[]) { 15. new TestAll().makeThings(); 16. // ...code continues on 17. } 18. void makeThings() { 19. TestA test = new TestA(); 20. } 21. } Which two statements are true after line 15, before main completes? (Choose two) A. Line 15 causes a stack overflow. B. An exception is thrown at runtime. C. The object referenced by a is eligible for garbage collection. D. The object referenced by b is eligible for garbage collection. E. The object referenced by a is not eligible for garbage collection. F. The object referenced by b is not eligible for garbage collection.

C, D

for (int i =0; i <3; i++) { switch(i) { case 0: break; case 1: System.out.print(“one “); case 2: System.out.print(“two “); case 3: System.out.print(“three “); } } System.out.println(“done”); What is the result? A. done B. one two done C. one two three done D. one two three two three done E. Compilation fails.

D. one two three two three done

Which three statements are true? (Choose three) A. The default constructor initializes method variables. B. The default constructor has the same access as its class. C. The default constructor invoked the no-arg constructor of the superclass. D. If a class lacks a no-arg constructor, the compiler always creates a default constructor. E. The compiler creates a default constructor only when there are no other constructors for the class.

B. The default constructor has the same access as its class.
C. The default constructor invoked the no-arg constructor of the superclass.
E. The compiler creates a default constructor only when there are no other constructors
for the class.

Which three statements are true? (Choose three) A. Assertion checking is typically enabled when a program is deployed. B. It is never appropriate to write code to handle failure of an assert statement. C. Assertion checking is typically enabled during program development and testing. D. Assertion checking can be selectively enabled or disable an a per-package basis, but not on a per-class basis. E. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

B, C, E

10. public Object m() { 11. Object o = new Float(3.14F); 12. Object [] oa = new Object[1]; 13. oa[0] = o; 14. o = null; 15. oa[0] = null; 16. print 'return 0'; 17. } When is the Float object, created in line 11, eligible for garbage collection? A. Just after line 13. B. Just after line 14. C. Just after line 15. D. Just after line 16 (that is, as the method returns).

C. Just after line 15

11. public void test(int x) { 12. int odd = x%2; 13. if (odd) { 14. System.out.println(odd); 15. } else { 16. System.out.println(even); 17. } 18. } Which statement is true? A. Compilation fails. B. odd will always be output. C. even will always be output. D. odd will be output for odd values of x, and even for even values. E. even will be output for add values of x, and odd for even values.

A. Compilation fails.

1. class Super { 2. public int getLenght() { return 4; } 3. } 4. 5. public class Sub extends Super { 6. public long getLenght() { return 5; } 7. 8. public static void main(String[] args) { 9. Super sooper = new Super(); 10. Sub sub = new Sub(); 11. System.out.println( 12. sooper.getLenght() + ","+ sub.getLenght() ); 13. } 14. } What is the output? A. 4,4 B. 4,5 C. 5,4 D. 5,5 E. Compilation fails.

E. Compilation fails.

1. public class Test { 2. public static void main(String[] args) { 3. int x = 0; 4. assert (x > 0): "assertion failed"; 5. System.out.println("finished"); 6. } 7. } What is the result? A. finished B. Compilation fails. C. An AssertionError is thrown. D. An AssertionError is thrown and finished is output.

A. finished

1) The Java interpreter is used for the execution of the source code? a) True b) False

true

5) Java supports multidimensional arrays? a)False b)True

true

6)An array of arrays can be created. a)True b)False

true

12) Strings are instances of the class String? a)True b)False

true

8)When a string literal is used in the program, Java automatically creates instances of the string class? a)True b)False

true

9)Which of the following are primitive types? 1. byte 2. String 3. integer 4. Float

byte

10)What is the range of the char type? 1. 0 to 2 16 2. 0 to 2 15 3. 0 to 2 16-1 4. 0 to 2 15-1

0 to 2 15-1

11)Converting of primitive types to objects can be explicitly. a)True b)False

false

Output of following(JDK11) var str = "abc"; var repeated = str.repeat(3); System.out.println(repeated); 1. abcabcabc 2. abcabcabcabc 3.abc 4.Syntax error

abcabcabc

Output of following code var multiline = "I am is Rajesh Kumar"; Stream< String > stream = multiline.lines(); stream.map(String::toUpperCase) .forEach(System.out::println); 1. I AM Rajesh Kumar 2. Syntax error

1