JAVA Theory Questions ( Exam – I)
1. How many objects and reference variables are created by the following lines of code?
Employee emp1, emp2, emp3;
emp1 = new Employee();
Employee emp4 = new Employee() ;
A) Two objects and four reference variables.
B) Three objects and two reference variables.
C) Four objects and two reference variables.
D) Two objects and two reference variables.
2. If a variable is declared as protected, then it can be used in _______
A) Any class of any package
B) Only classes of same package
C) Only subclass in that package
D) Classes of same package and sub classes any where
3. Given the following piece of code:
public class Company{
public abstract double calculateSalaries();
}
Which of the following statements is true?
A) The keywords public and abstract cannot be used together.
B) The method calculateSalaries() in class Company must have a body
C) You must add a return statement in method calculateSalaries().
D) Class Company must be defined abstract.
4. Given these classes:
public class Person{
public void speak(){ System.out.print("I am a Person "); }
}
public class Student extends Person {
public void speak(){ System.out.print("I am a Student "); }
}
what is the result of this piece of code:
public class Test{
public static void main(String args[]){
Person p = new Student();
p.speak();
}}
A) I am a Person
B) I am a Student
C) I am a Person I am a Student
D) I am a Student I am a Person
5. Given the following declarations
String s1=new String("Hello")
String s2=new String("there");
String s3=new String();
Which of the following are legal operations?
A) s3=s1 + s2;
B) s3=s1-s2;
C) s3=s1 & s2;
D) s3=s1 && s2;
6. Which one of these events will cause a Thread to die?
A) Call of sleep() method.
B) End of execution of wait() method.
C) End of execution of run() method.
D) End of execution of Thread’s constructor.
7. Which of the following are wrapper classes for primitive types?
A) java.lang.String
B) java.lang.Void
C) java.lang.Object
D) None of the above
8. Super class of all exceptions is ___________
A) Error
B) Throwable
C) Object
D) Exception
9. What type of exception is thrown by parseInt() if it gets illegal data?
A) Arithmetic Exception
B) RunTimeException
C) NumberFormatException
D) NumberError
10. Interfaces in Java can __________
A) Implement multiple interfaces
B) Extend only one interface
C) Extend multiple interfaces
D) None of the above.
11. class Person{ public void talk(){} }
public class Test{
public static void main(String args[]){
Person p = null;
try{
p.talk();
} catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
} catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything went fine. ");
}
}
what will be the result?
(A) There is a NullPointerException. Everything went fine.
(B) There is a NullPointerException.
(C ) There is a NullPointerException. There is an Exception.
(D) This code will not compile, because in Java there are no pointers.
12. Which of the following declarations is correct?
(A) boolean b = TRUE;
(B) byte b = 255;
(C ) String s = ‘null’;
(D) int i = new Integer(“56”);
13. Wrapper classes are found in _____ package
(A) java.lang B) java.util C) java.io D) java.net
14. Consider the following code snippet
String river = new String(“Godavari”);
System.out.println(river.length());
What is printed?
A. Godavari B. 7 C. 8 D. 9
15. Consider
public class MyClass{
public MyClass(){/*code*/}
// more code...
}
To instantiate MyClass, you would write?
A. MyClass mc = new MyClass();
B. MyClass mc = MyClass();
C. MyClass mc = MyClass;
D. MyClass mc = new MyClass;
16. What will be the output of the following program?
public class CommandArgsTwo {
public static void main(String[] args) {
int x;
x = args.length;
for (int y=1; y<=x; y++)
{
System.out.print(" " + args[y]);
}
}
}
and the command-line invocation is
> java CommandArgsTwo 1 2 3
A) 0 1 2 B) 1 2 3
C) Compiler Error D) Runtime Exception (ArrayIndexOutOfBounds)
17. Which is the implicit access modifier for an interface method?
A) private B) default C) public D) protected
18. public class CDAC{
public static void main(String a[]){
String s1 = "Sun";
System.out.println(s1.substring(5));
}
}
What is output?
A. -1 B. 0
C. StringIndexOutOfBoundsException D. ArrayIndexOutOfBoundsException
19. Which of the following modifiers can be applied to a constructor?
A. Private B. abstract C. volatile D. All of the above
20. . Given the following piece of code:
public class Test {
public static void main(String args[]) {
int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ) {
System.out.print(" " + i + " " + j );
}
System.out.print(" " + i + " " + j );
}
} what will be the result?
(A) 0 6 1 7 2 8 3 8 (B) 0 6 1 7 2 8 3 9
(C) 0 5 1 5 2 5 3 5 (D) Compilation fails