You are on page 1of 7

Test: Java Programming Midterm Exam

1. Given the following output from the Minor GC:


[GC [DefNew: 4032K->64K(4032K), 0.0429742 secs] 9350K->7748K(32704K), 0.0431096 secs]
Which of the following statements is TRUE?
Entire heap is 9350k.
Young Generation usage decreased by 3968k. (*)
The pause time spent in Young Generation is 0.0431096
The size of Eden space is 4032k.

2. Which of the following allows the programmer to destroy an object referenced by x?


x.remove();
x.finalize();
x.delete();
Only the garbage collection system can destroy an object. (*)

3. Given the following code snippet:


String str = new String("Hello");
The "Hello" String literal will be located in which memory area in the JVM during runtime?
In the Heap area of the run-time data area in the JVM.
In the Method area of the run-time data area in the JVM.
In the Stack area of the run-time data area in the JVM.
In the constant pool area of the run-time data area in the JVM. (*)

4. Which of the following statements is NOT TRUE about the JVM?


The JVM is a virtual Machine that acts as an intermediary layer between the Java Application
and the Native Operating System.
The JVM reads byte code from the class file, and generates machine code.
The JVM does not understand the Java language specification.
The JVM reads Java source code, and then translates it into byte code. (*)

5. Java allows the same Java program to be executed on multiple operating systems.
True (*)
False

6. Which of the following converts a human-readable file into a platform-independent code file
in Java?
JRE
JDK
javac command (*)
java command

7. HotSpot has an HSDIS plugin to allow disassembly of code.


True (*)
False

8. Which of the following statements is NOT TRUE for the jdb command?
jdb can display the source code.
jdb can set the break pont for the program.
jdb can dump the stack of the current thread.
jdb can track the GC activity of the program. (*)
9. Which of the following commands can be used to monitor the Java Virtual Machine
statistics?
jstat (*)
javap
javac
jmap

10. Like in the Java source code file, one Java class file can contain multiple class definitions.

True
False (*)

11. Given the following class structure:


public class Shape{
void foo(){}
}
public class Circle extends Shape{
void draw(){}
}
Which of the following statements is TRUE?

The foo method definition appears in the Circle class.


The Circle class contains both the foo and draw method definitions.
The foo method definition is only contained in the Shape class. (*)
If a Circle object is instantiated, the constructor of Circle will call the constructor of Shape.

12. The bytecode for a Java method is located in which structure in the Java class file?
magic
access_flags
method_info (*)
major_version

13. To inspect bytecode, which option is used with the javap command to disassemble the class
file?
-a
-b
-c (*)
-d

14. Choose which opcode is used to push an int constant 5 onto the operand stack.
iconst_5 (*)
idc5
iload_5
iaload_5
iinc5

15. Choose which opcode is used to fetch a field from object.


istore
idc
pop
bipush
getfield (*)
16. Which of the following statements is NOT TRUE for the Class.forName("HelloClass") method?
(Choose three)

public class Foo{


public void test(){
Class.forName("HelloClass");
}
}
The forName() method does not initialize the HelloClass. (*)
The forName() method returns the Class object associated with the HelloClass.
The forName() method does not load the HelloClas class into the Java Runtime. (*)
In this example, the Class.forName("HelloClass") will use the ClassLoader which loads the Foo
class.
The forName method will instantiate a HelloClass object. (*)

17. Which of the following exceptions is thrown by the loadClass() method of ClassLoader class?

IOException
SystemException
ClassFormatError
ClassNotFoundException (*)

18. Which of the following from ClassLoader will load the rt.jar, the Java core clsses which are
present in the java.* package?
Extension Class Loader
Custom Class Loader
Bootstrap Class Loader (*)
Application Class Loader

19. Which ofthe following declarations are wrong?(Choose Three)


protected private int id; (*)
abstract final class Hello{} (*)
public abstract class Student{}
abstract private void act(){} (*)

20. Examine the partial class declaration below:


class Foo{
public Foo(String s,int i ){
this(i);
}
public Foo(int i){
}
public Foo(int i,int j){
}
}

Which of following statements can not be used to create a instance of Foo?


Foo f=new Foo(1,2);
Foo f=new Foo("java",1);
Foo f=new Foo(1);
Foo f=new Foo(); (*)
21. Which of the following types are primitive data types? (Choose Two)
double (*)
String
boolean (*)
Integer

22. Which statement is a syntactically correct way to declare an Array?


int[5] id={1,2,3,4,5};
int id[]; (*)
int i[1];
int i[1] id;

23. Which combination of the following overload the Student constructor?(Choose Two)
protected int Student(){}
public void Student(int x, int y){}
public Object Student(int x,int y){}
public Student(){} (*)
public Student(int x,int y){} (*)

24. A class can be extended by more than one class. True or False?
True (*)
False

25. What is the output from the following code?

int x=5;
if (x>6)
System. out. println("x>l");
else if (x>=5)
System. out .println("x>=5");
else if (x<10)
System. out. println("x<10");
else
System.out.println("none of the above");
x>6
x>=5 (*)
x<10
None of these answers are correct

26. Why should you not use assertions to check parameters?


Assertions do not work on parameters.
Assertions can be disabled at run time which may cause unexpected results in your
assertions. (*)
It is hard to assume expected values for parameters.
Not all methods have parameters, therefore assertions should never be used on parameters.

27. Which of the following is the correct way to throw cumstom ServerException?
throw ServerException
throws ServerException
throw new ServerException() (*)
raise ServerException
28. What is the output from the following code snippet?

public static void main(String[] args){


try{
String[] s=null;
s[0]="Java";
System.out.println(s[0]);
}catch(Exception e) {
System.out.println("Exception");
}catch(NullPointerException e){
System.out.println("NullPointerException");
}
NullPointerException
Compile fails (*)
Exception
Java

29. What is special about including a resource in a try statement?(Choose Two)


An error will be thrown if the resources does not open. (*)
The program will fail if the resource does not open.
The resources will auto-close. (*)

30. When do you use try-catch statements?


If you want to switch different values for a certain variable.
Every time you would like to assign a new value to a variable that is being asserted.
When you want to exit your code before an exception is caught.
When you want to handle an exception. (*)

31. In what order do multiple catch statements execute?


The order they are declared in ( most specific first). (*)
They all execute at the same time.
The order they are declared in (most general first).
None of them execute since you cannot have multiple catch statements.

32. This is correct syntax for catching an exception:


try(inputStream = "missingfile.txt");
catch(exception e);
True or false?
True
False (*)

33. Which of the following statements is false?


In an Array you need to know the length and the current number of elements stored.
An ArrayList can grow and shrink dynamically as required.
An ArrayList can store multiple object types.
An ArrayList has a fixed length. (*)

34. Unit testing is the phase in software testing in which individual software modules are
combined and tested as a whole. True or false?
True
False (*)
35. Which of the following are important to your survival as a programmer?
Being good at reading code.
Looking for opportunities to read code.
Being good at testing.
All of these. (*)

36. Examine the following code snippet. What is this an example of?

public class Car extends Vehicle {


public Car() {
...
}
}
Polymorphism
Encapsulation
Inheritance (*)
Comments

37. Reading great code is just as important for a programmer as reading great books is for a
writer. True or false?
True (*)
False

38. Which of the following statements about arrays and ArrayLists in Java are true?
I. An Array has a fixed length.
II. An Array can grow and shrink dynamically as required.
III. An ArrayList can store multiple object types.
IV. In an ArrayList you need to know the length and the current number of elements stored.

I and III only (*)


II and IV only
I, II, and III only
I, II, III and IV
None of these

39. Interfaces define what?


Constants and all methods with implementations
Some methods with implementations
All method definitions without any implementations (*)
Variables and methods
All methods with implementations

40. Which two statements are equivalent to line 2?


(Choose Two)
1. public interface Account{
2. int accountID=100;
3. }
static int accountID=100; (*)
Abstract int accountID=100;
protected int accountID=100;
Final int accountID=100; (*)
private int accountID=100;
41. Modeling business problems requires understanding the interaction between interfaces,
abstract and concrete classes, subclasses, and enum classes.
True (*)
False

42. Immutable classes can be subclassed.


True or false?
True
False (*)

43. Making a class immutable means:


That it is directly subclassed from the Object class
To create an instance of the class.
To create a sub class from a parent class
That it cannot be extended. (*)

44. An interfaces can declare public constants.


True or False?
True (*)
False

45. Virtual method invocation requires that the superclass method is defined as which of the
following?
A public method. (*)
A public static method.
A public final method.
A default final method.
A private final method.

46. An abstract class can implement its methods. True or false?


True (*)
False

47. The instanceof operator is a boolean comparison operator. True or false?


True (*)
False

48. The instanceof operator enables to discover the type of object it was invoked upon.
True or false?
True (*)
False

49. The instanceof operator works with class instances and primitive data types.
True or false?
True
False (*)

50. The instanceof operator allows you to determine the type of an object.
True or false?
True (*)
False

You might also like