Java Programming Fundamentals

Test your knowledge of Java programming basics including arrays, classes, constructors, inheritance, interfaces, exceptions, and object references.

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

What is the output of the following code when compiled and run? Select one correct answer. public class Question02 { public static void main(String[] args){ int j = 017; int i = (byte)j >> 2; //line 1 System.out.println(Integer.toBinaryString(i)); //line 2 } }

  1. Prints 3
  2. Error during compilation at line 1
  3. Prints 11
  4. Prints 0
Question 2 Multiple Choice (Single Answer)

What is the output of the following code when compiled and run? Select one correct answer. import java.io.*; public class Question05 { public static void main(String[] args) { Question05Sub myref = new Question05Sub(); try{ myref.test(); }catch(IOException ioe){} } void test() throws IOException{ System.out.println("In Question05"); throw new IOException(); } } class Question05Sub extends Question05 { void test() { System.out.println("In Question05Sub"); } }

  1. Prints: In Question05Sub
  2. Prints: In Question05
  3. Prints:In Question05In Question05Sub
  4. The code does not compile.
Question 3 Multiple Choice (Multiple Answers)

Select three correct statements about the following code. public class Question20 { public static void main(String[] args) { Question20 myref = new Question20Sub(); try{ myref.test(); }catch(Exception e){} } void test() throws Exception{ System.out.println("In Question20"); throw new Exception(); } } class Question20Sub extends Question20 { void test() { System.out.println("In Question20Sub"); } }

  1. The try-catch block that encloses myref.test(); is mandatory for the code to compile.
  2. Prints: In Question20
  3. Prints: In Question20Sub
  4. Method test() in class Question20Sub has no obligation to declare a throws clause.
  5. An exception is thrown at runtime.
Question 4 Multiple Choice (Single Answer)

What is the output of the following code when compiled and run? Select one correct answer. public class Question41{ public static void main(String[] args){ Object[] obj = new Object[3]; for(int i=0;i<obj.length;i++) obj[i] = (i%2==0)?new Object():obj[i-1]; if(obj[0] == obj[1] & (obj[1]=obj[2])!=null) System.out.print("1 "); if(obj[1] == obj[2] && (obj[2]=obj[0])!=null) System.out.print("2 "); if(obj[1] == obj[0] || (obj[0]=obj[1])==null) System.out.print("3 "); if(obj[2] == obj[0] | (obj[0]=obj[2])!=null) System.out.print("4 "); System.out.println((obj[0]==obj[1])+" "+(obj[1]==obj[2])+" "+(obj[0]==obj[2])); } }

  1. Compilation error.
  2. Prints: 1 3 4 true false true
  3. Prints: 1 2 4 false false true
  4. Prints: 3 4 true true true
  5. None of the above.
Question 5 True/False

When an instance of a class, or object, is specified as a parameter to a method, a reference to the said object is passed to the method.

  1. True
  2. False
Question 6 True/False

All interface methods must be declared as public when implemented in a class.

  1. True
  2. False
Question 7 True/False

A method in a class declared as static may be invoked simply by using the name of the method alone.

  1. True
  2. False
Question 8 True/False

A static method can refer to any instance variable of the class.

  1. True
  2. False
Question 9 True/False

Each method in a class must have a unique name.

  1. True
  2. False
Question 10 Multiple Choice (Multiple Answers)
  1. Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
  1. a. arr.length
  2. b. arr.length - 1
  3. c. arr.size
  4. d. arr.size - 1
  5. e. arr.length()
Question 11 Multiple Choice (Multiple Answers)
  1. Which of these statements are legal. Select the three correct answers.
  1. a. int arr[][] = new int[5][5];
  2. b. int []arr[] = new int[5][5];
  3. c. int[][] arr = new int[5][5];
  4. d. int[] arr = new int[5][];
  5. e. int[] arr = new int[][5];
Question 12 Multiple Choice (Multiple Answers)
  1. Select all correct declarations, or declaration and initializations of an array?
  1. a. String str[];
  2. b. String str[5] = new String[5];
  3. c. String str[] = new String[] {"string1", "string2", "string3", "string4", "string5
  4. d. String str[] = {"string1","string2", "string3", "string4", "string5"};
Question 13 Multiple Choice (Multiple Answers)

4 Which of the following are the java keywords?

  1. 1. final
  2. 2. Abstract
  3. 3. Long
  4. 4. static
Question 14 Multiple Choice (Multiple Answers)

5 What will be printed when you execute the code? class A { A() { System.out.println("Class A Constructor"); } } public class B extends A { B() { System.out.println("Class B Constructor"); } public static void main(String args[]) { B b = new B(); } }

  1. 1. Class A Constructor followed by Class B Constructor
  2. 2. Class B Constructor followed by Class A Constructor
  3. 3. Compile time error
  4. 4. Run time error
Question 15 Multiple Choice (Multiple Answers)

6 What will happen when you compile the following code? 1. package sources; 2. public class firstjava { 3. public static void main(String args[]) 4. { 5. private int a[] ={ 4, 4 }; 6. public int b=1; 7. a[b]=b=0; 8. System.out.println("value of b & a[0] & a[1]" + b + a[0] +a[1]); 9. } 10. }

  1. 1. Compilation error at line 2
  2. 2. Runtime error at line 7.
  3. 3. Compilation error at Line 7
  4. 4. Compiles and prints 0 0 4
  5. 5. Compiles and prints 0 4 0
Question 16 Multiple Choice (Multiple Answers)

7 What will be the result of attempting to compile and run the following code? abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase { public static void main(String argv[]){ int[] ar=new int[5]; for(i=0;i < ar.length;i++) System.out.println(ar[i]); } }

  1. 1. a sequence of 5 0's will be printed
  2. 2. Error: ar is used before it is initialized
  3. 3. Error Mine must be declared abstract
  4. 4. IndexOutOfBoundes Error
Question 17 Multiple Choice (Multiple Answers)

8 What will happen if you attempt to compile and run the following code? Integer ten=new Integer(10); Long nine=new Long (9); System.out.println(ten + nine); int i=1; System.out.println(i + ten);

  1. a. 19 followed by 20
  2. b. 19 followed by 11
  3. c. Compile time error
  4. d. 10 followed by 1
Question 18 Multiple Choice (Multiple Answers)

9 If you run the code below, what gets printed out? String s=new String("Bicycle"); int iBegin=1; char iEnd=3; System.out.println(s.substring(iBegin,iEnd));

  1. a. Bic
  2. b. ic
  3. c. icy
  4. d. error: no method matching substring(int,char)
Question 19 Multiple Choice (Multiple Answers)

10 What modifiers would be legal at XX in the following code? public class MyClass1 { public static void main(String argv[]){ } /*Modifier at XX */ class MyInner {} }

  1. a. public
  2. b. private
  3. c. static
  4. d. friend
Question 20 Multiple Choice (Multiple Answers)

11 What will the following code print out? public class Oct{ public static void main(String argv[]){ Oct o = new Oct(); o.amethod(); } public void amethod(){ int oi= 012; System.out.println(oi); } }

  1. A 12
  2. B 012
  3. C 10
  4. D 10.0