Java Programming Fundamentals
Test your knowledge of Java programming basics including arrays, classes, constructors, inheritance, interfaces, exceptions, and object references.
Questions
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 } }
- Prints 3
- Error during compilation at line 1
- Prints 11
- Prints 0
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"); } }
- Prints: In Question05Sub
- Prints: In Question05
- Prints:In Question05In Question05Sub
- The code does not compile.
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"); } }
- The try-catch block that encloses myref.test(); is mandatory for the code to compile.
- Prints: In Question20
- Prints: In Question20Sub
- Method test() in class Question20Sub has no obligation to declare a throws clause.
- An exception is thrown at runtime.
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])); } }
- Compilation error.
- Prints: 1 3 4 true false true
- Prints: 1 2 4 false false true
- Prints: 3 4 true true true
- None of the above.
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.
- True
- False
All interface methods must be declared as public when implemented in a class.
- True
- False
A method in a class declared as static may be invoked simply by using the name of the method alone.
- True
- False
A static method can refer to any instance variable of the class.
- True
- False
Each method in a class must have a unique name.
- True
- False
- Given a one dimensional array arr, what is the correct way of getting the number of elements in arr. Select the one correct answer.
- a. arr.length
- b. arr.length - 1
- c. arr.size
- d. arr.size - 1
- e. arr.length()
- Which of these statements are legal. Select the three correct answers.
- a. int arr[][] = new int[5][5];
- b. int []arr[] = new int[5][5];
- c. int[][] arr = new int[5][5];
- d. int[] arr = new int[5][];
- e. int[] arr = new int[][5];
- Select all correct declarations, or declaration and initializations of an array?
- a. String str[];
- b. String str[5] = new String[5];
- c. String str[] = new String[] {"string1", "string2", "string3", "string4", "string5
- d. String str[] = {"string1","string2", "string3", "string4", "string5"};
4 Which of the following are the java keywords?
- 1. final
- 2. Abstract
- 3. Long
- 4. static
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. Class A Constructor followed by Class B Constructor
- 2. Class B Constructor followed by Class A Constructor
- 3. Compile time error
- 4. Run time error
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. Compilation error at line 2
- 2. Runtime error at line 7.
- 3. Compilation error at Line 7
- 4. Compiles and prints 0 0 4
- 5. Compiles and prints 0 4 0
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. a sequence of 5 0's will be printed
- 2. Error: ar is used before it is initialized
- 3. Error Mine must be declared abstract
- 4. IndexOutOfBoundes Error
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);
- a. 19 followed by 20
- b. 19 followed by 11
- c. Compile time error
- d. 10 followed by 1
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));
- a. Bic
- b. ic
- c. icy
- d. error: no method matching substring(int,char)
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 {} }
- a. public
- b. private
- c. static
- d. friend
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); } }
- A 12
- B 012
- C 10
- D 10.0