Вы находитесь на странице: 1из 72

More chiragkhade@gmail.

com Dashboard Sign Out

section 1-5 oracle java programming

MONDAY, OCTOBER 15, 2018 ABOUT ME


karre resakl
karre jawa oracle java programming midterm and section 1 -5 View my complete profile

java programming
Section 1
(Answer all questions in this section)
10. The function of Garbage Collection in Java is: Mark for Review BLOG ARCHIVE
(1) Points ▼ 2018 (1)
▼ October (1)
karre jawa oracle java programming
The JVM uses GC to clear the program output. midterm and sec...

The JVM GC deletes all unused Java files on the system.

Memory occupied by objects with no reference is automatically reclaimed for reuse. (*)

As a Java programmer, we have to call the GC function specifically in order to manage the Java
Memory.

1. Which of the following statements is NOT true of the Java programming language? Mark for
Review
(1) Points

All source code is written in plain text files with the extension .java.

Java source code files are compiled into .class files by the javac command.

The javac command can be used to run a Java application. (*)

A .class file contains platform independent bytecode.

Correct Correct

2. Which of the following statements describe the Java programming language? Mark for
Review
(1) Points

Java is a high-level programming language.

The Java programming language includes a garbage collection feature.

Java is an object oriented programming language.

All of the above (*)

Correct Correct
3. Given the java snippet below:

public class Foo{


int x;
public void testFoo(){
int y=100;
}
}

Which of the following statements is TRUE? Mark for Review


(1) Points

Compile error, as the variable x is not initialized.

Variable x resides in the stack area, and variable y resides in the heap of the JVM.

Variable x resides in the heap area, and variable y resides in the stack of the JVM (*)

Variable x stays in the heap area, and variable y resides in the method area of the JVM.

Correct Correct

4. Which of the following converts a human-readable file into a platform-independent code file
in Java? Mark for Review
(1) Points

JRE

JDK

javac command (*)

java command

Incorrect Incorrect. Refer to Section 1 Lesson 1.

5. Java allows the same Java program to be executed on multiple operating systems. Mark for
Review
(1) Points

True (*)

False

Correct Correct
6. Which of the following statements describe Java technology? Mark for Review
(1) Points

It is a programming language.

It is a development environment.
It is a deployment environment.

All of the above (*)

Incorrect Incorrect. Refer to Section 1 Lesson 1.

7. During runtime, the Java platform loads classes dynamically as required. Mark for Review
(1) Points

True (*)

False

Correct Correct

8. One of the primary goals of the Java platform is to provide an interpreted, just-in-time run
time environment. Mark for Review
(1) Points

True (*)

False

Incorrect Incorrect. Refer to Section 1 Lesson 1.

9. 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? Mark for Review
(1) Points

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.

Incorrect Incorrect. Refer to Section 1 Lesson 2.

10. 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? Mark
for Review
(1) Points

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. (*)

Incorrect Incorrect. Refer to Section 1 Lesson 2.

11. Given the following code snippet:

String str = new String("Hello");


The str variable will be located in which memory area in the JVM during runtime? Mark for
Review
(1) Points

str will stay in the heap area of the run-time data area in the JVM.

str will stay in the method area of the run-time data area in the JVM.

str will stay in the stack area of the run-time data area in the JVM. (*)

str will stay in the heap area of the constant pool run-time data area in the JVM.

Incorrect Incorrect. Refer to Section 1 Lesson 2.

12. In which area of heap memory are newly created objects stored? Mark for Review
(1) Points

Survivor Space 0

Survivor Space 1

Eden (*)

Tenured

Incorrect Incorrect. Refer to Section 1 Lesson 2.

13. Which of the following allows the programmer to destroy an object referenced by x? Mark
for Review
(1) Points

x.remove();

x.finalize();

x.delete();

Only the garbage collection system can destroy an object. (*)

Correct Correct
14. Which of following statements describes Parallel and Serial Garbage collection? Mark for
Review
(1) Points

A Serial garbage collector uses multiple threads to manage heap space.

A Parallel garbage collector uses multiple threads to manage heap space. (*)

A Parallel garbage collector uses multiple threads to manage stack space.

A Serial garbage collector uses multiple threads to manage stack space.

Incorrect Incorrect. Refer to Section 1 Lesson 2.

15. Which of the following statements is NOT TRUE for the JVM heap? Mark for Review
(1) Points

Java Developer can explicitly allocate and deallocate the Heap Memory. (*)

The Heap can be managed by the Garbage Collector.

The Heap can be shared among all Java threads.

Class instance and arrays are allocated in the Heap Memory.

Correct Correct
1. Given the java snippet below:

public class Foo{


int x;
public void testFoo(){
int y=100;
}
}

Which of the following statements is TRUE? Mark for Review


(1) Points

Compile error, as the variable x is not initialized.

Variable x resides in the stack area, and variable y resides in the heap of the JVM.

Variable x resides in the heap area, and variable y resides in the stack of the JVM (*)

Variable x stays in the heap area, and variable y resides in the method area of the JVM.

Correct Correct

2. Which of the following statements describe the Java programming language? Mark for
Review
(1) Points
Java is a high-level programming language.

The Java programming language includes a garbage collection feature.

Java is an object oriented programming language.

All of the above (*)

Correct Correct

3. During runtime, the Java platform loads classes dynamically as required. Mark for Review
(1) Points

True (*)

False

Correct Correct

4. Which of the following statements is NOT TRUE about the JVM? Mark for Review
(1) Points

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. (*)

Incorrect Incorrect. Refer to Section 1 Lesson 1.

5. Where does an object of a class get stored? Mark for Review


(1) Points

Stack area

Method area

In the file

In the database

Heap area (*)


6. Java allows the same Java program to be executed on multiple operating systems. Mark for
Review
(1) Points

True (*)
False

Correct Correct

7. Which of the following converts a human-readable file into a platform-independent code file
in Java? Mark for Review
(1) Points

JRE

JDK

javac command (*)

java command

Correct Correct

8. One of the primary goals of the Java platform is to provide an interpreted, just-in-time run
time environment. Mark for Review
(1) Points

True (*)

False

Correct Correct

9. Which of following statements describes Parallel and Serial Garbage collection? Mark for
Review
(1) Points

A Serial garbage collector uses multiple threads to manage heap space.

A Parallel garbage collector uses multiple threads to manage heap space. (*)

A Parallel garbage collector uses multiple threads to manage stack space.

A Serial garbage collector uses multiple threads to manage stack space.

Correct Correct

10. 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? Mark for Review
(1) Points

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.

Correct Correct

11. Which of the following statements is NOT TRUE for the JVM heap? Mark for Review
(1) Points

Java Developer can explicitly allocate and deallocate the Heap Memory. (*)

The Heap can be managed by the Garbage Collector.

The Heap can be shared among all Java threads.

Class instance and arrays are allocated in the Heap Memory.

Correct Correct

12. 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? Mark
for Review
(1) Points

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. (*)

Incorrect Incorrect. Refer to Section 1 Lesson 2.

13. Given the following output from the Minor GC:


[GC [DefNew: 4032K->64K(4032K), 0.0429742 secs] 9350K->7748K(32704K), 0.0431096 secs]
What estimated percentage of the Java objects will be promoted from Young space to Tenured
space? Mark for Review
(1) Points

0.2

0.4

0.6 (*)

0.8
0.9

Incorrect Incorrect. Refer to Section 1 Lesson 2.

14. Which of the following allows the programmer to destroy an object referenced by x? Mark
for Review
(1) Points

x.remove();

x.finalize();

x.delete();

Only the garbage collection system can destroy an object. (*)

Correct Correct

15. Given the following code snippet:

String str = new String("Hello");


The str variable will be located in which memory area in the JVM during runtime? Mark for
Review
(1) Points

str will stay in the heap area of the run-time data area in the JVM.

str will stay in the method area of the run-time data area in the JVM.

str will stay in the stack area of the run-time data area in the JVM. (*)

str will stay in the heap area of the constant pool run-time data area in the JVM.

Correct Correct

Section 2
(Answer all questions in this section)

1. Which of the following commands allows a developer to see the effects of a running java
application on memory and CPU? Mark for Review
(1) Points

javac

jvisualvm (*)

java

javap

Correct Correct
2. HotSpot has an HSDIS plugin to allow disassembly of code. Mark for Review
(1) Points

True (*)

False

Correct Correct

3. Which of the following commands is used to launch a java program? Mark for Review
(1) Points

javac

jvisualvm

java (*)

javap

Incorrect Incorrect. Refer to Section 2 Lesson 1.

4. Before we can use the jsat tool we first have to use the jps tool to obtain JVM process id
numbers. Mark for Review
(1) Points

True (*)

False

Correct Correct

5. Given the following information in the jdb tool, jdb paused at line 11:

9 public static void method1(){


10 x=100;
11 }

public static void method1();


Code:
0: bipush 100
2: putstatic #7 //Field x:I
5: return

Which statement is true?


Step completed: "thread-main", Example.method1(), line=11 bci=5
Mark for Review
(1) Points

The line=11 means the jdb executed line 11 bytecode in the method1 method.

The bci=5 means the jdb executed the last bytecode instruction in the method1 method. (*)
The bci=5 means the jdb executed 5 lines of the source code.

From the bytecode, we can assume the Variable x is an instance variable.

Correct Correct

6. The javac command can be used to display native code in Java Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 2 Lesson 1.

7. The jsat tool can be used to monitor garbage collection information. Mark for Review
(1) Points

True (*)

False

3. Which of the following structures are contained in a Java class file? Mark for Review
(1) Points

minor_version

major_version

access_flags

All of the above (*)


The bytecode for a Java method is located in which structure in the Java class file? Mark for
Review
(1) Points

magic

access_flags

method_info (*)

major_version

Which structure in the Java class file contains the line number information for the original
source file? Mark for Review
(1) Points

method_info (*)

this_class

filed_info
cp_info
Which of the following commands can be used to translate Java source code into bytecode?
Mark for Review
(1) Points

java

javac (*)

jdb

jstat

Correct Correct

8. Which of the following commands can be used to monitor the Java Virtual Machine
statistics? Mark for Review
(1) Points

jstat (*)

javap

javac

jmap

Incorrect Incorrect. Refer to Section 2 Lesson 1.

9. Which of the following statements is NOT TRUE for the jdb command? Mark for Review
(1) Points

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. (*)

Incorrect Incorrect. Refer to Section 2 Lesson 1.

10. The class file contains the definition it inherits from the superclass. Mark for Review
(1) Points

True

False (*)
Incorrect Incorrect. Refer to Section 2 Lesson 2.

11. Given the following instance variable:


public void foo(){
int i=888888;
}

Which of the following statements is NOT TRUE? Mark for Review


(1) Points

The variable i is a local variable.

The 888888 is an integer literal. After compile, the number will stay in the constant pool.

The variable i and the literal 888888 are stored in the method_info. (*)

The field descriptor for the variable i is I.

Correct Correct

12. The attributes_count item indicates how many attributes are contained within a method.
Mark for Review
(1) Points

True (*)

False

10. Like in the Java source code file, one Java class file can contain multiple class definitions.
Mark for Review
(1) Points

True

False (*)

Correct Correct

13. Given the following declaration of the method test:


public static void test(String s, int i);

Which of the following is the descriptor of the test method in the class file? Mark for Review
(1) Points

(java/lang/String;int)V

(Ljava/lang/String;I)V (*)

V(Ljava/lang/String;I)

(Ljava/lang/String;java.lang.Integer)V

Incorrect Incorrect. Refer to Section 2 Lesson 2.


14. Given the following class structure:
public class Shape{
void foo(){}
}
public class Circle extends Shape{
void draw(){}
}
Which of the following statements is TRUE? Mark for Review
(1) Points

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.

Incorrect Incorrect. Refer to Section 2 Lesson 2.

15. In a valid Java class file, the magic number is always: Mark for Review
(1) Points

42

CAFEBABE (*)

1.618

BABECAFE

Incorrect Incorrect. Refer to Section 2 Lesson 2.


Section 3
(Answer all questions in this section)

1. opcode invokespecial is used to invoke an instance initialization method. Mark for Review
(1) Points

True (*)

False

Correct Correct

2. Bytecode is an intermediate representation of a program, somewhere between source code


and machine code. Mark for Review
(1) Points

True (*)

False
Correct Correct

3. To inspect bytecode, which option is used with the javap command to disassemble the class
file? Mark for Review
(1) Points

-a

-b

-c (*)

-d

Incorrect Incorrect. Refer to Section 3 Lesson 1.

4. Choose which opcode is used to load an int from the local variable to the operand stack.
Mark for Review
(1) Points

aload

iload (*)

iaload

iconst

Incorrect Incorrect. Refer to Section 3 Lesson 1.

5. Java bytecode is generated by the javac command. Mark for Review


(1) Points

True (*)

False

Correct Correct

6. Choose which opcode is used to push an int constant 5 onto the operand stack. Mark for
Review
(1) Points

iconst_5 (*)

idc5

iload_5

iaload_5
iinc5

Incorrect Incorrect. Refer to Section 3 Lesson 1.

7. .class files are loaded into memory all at once, when a Java application is launched. Mark
for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 3 Lesson 2.

8. The Java developer can define a number of additional or custom classloaders. Mark for
Review
(1) Points

True (*)

False

Correct Correct

9. 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");
}
}
Mark for Review
(1) Points

(Choose all correct answers)

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. (*)

Incorrect Incorrect. Refer to Section 3 Lesson 2.

10. The process of linking involves which of the following processes? Mark for Review
(1) Points

verification

preparation

resolution

All of the above (*)

Incorrect Incorrect. Refer to Section 3 Lesson 2.

11. In the ClassLoader hierarchy, which of the following is the only class loader that does NOT
have a parent? Mark for Review
(1) Points

custom class loader

application class loader

bootstrap class loader (*)

extension class loader

Incorrect Incorrect. Refer to Section 3 Lesson 2.

12. Which of the following exceptions is thrown by the loadClass() method of ClassLoader
class? Mark for Review
(1) Points

IOException

SystemException

ClassFormatError

ClassNotFoundException (*)

Incorrect Incorrect. Refer to Section 3 Lesson 2.

13. The same class cannot be loaded by the JVM more than one time. Mark for Review
(1) Points

True (*)

False

Correct Correct
14. Which of the following from ClassLoader will load the rt.jar, the Java core clsses which are
present in the java.* package? Mark for Review
(1) Points

Extension Class Loader

Custom Class Loader

Bootstrap Class Loader (*)

Application Class Loader

Incorrect Incorrect. Refer to Section 3 Lesson 2.

15. The System or Application ClassLoader loads Java classes from the System Classpath.
This classpath is set by the CLASSPATH environment variable. Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 3 Lesson 2.

Section 4
(Answer all questions in this section)

1. What is the output from the following code?


String s= "a,b,c";
Scanner sc = new Scanner (s);
while (sc.hasNext())
System.out.print (sc.next() +" "); Mark for Review
(1) Points

a,b

ac

a,b,c (*)

abc

Correct Correct

2. You declare a method:


public void act(Student s){}

Which of following arguments can be passed into the method? (Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

Type of Object class


Interface

Type of Student class (*)

Type of the subclass of Student (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

3. Which three are valid declarations for a float value? (Choose Three) Mark for Review
(1) Points

(Choose all correct answers)

float f = -1; (*)

float f = 2.0f; (*)

float f = 3.0L;

float f = 0x345; (*)

float f = 1.0;

Incorrect Incorrect. Refer to Section 4 Lesson 1.

4. Which two statements prevent a method from being overriden? (Choose Two) Mark for
Review
(1) Points

(Choose all correct answers)

Void final act() {}

Final abstract void act() {}

Static final void act() {} (*)

Final void act() {} (*)

Static void act() {}

6. Which of the following operators are relational operators?(Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

"+="

"!=" (*)

">=" (*)
"="

Incorrect Incorrect. Refer to Section 4 Lesson 1.

7. Which of the following operators are logic operators?(Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

&& (*)

<=

>

! (*)

9. What is the output from the following code snippet?

int i=0, j=0;


i=i++;
j=i++;
System.out.println("i=" + i + " " + "j=" + j); Mark for Review
(1) Points

The code will compile and print "i=1 j=0" (*)

The code will compile and print "i=1 j=1"

The code will not compile.

The code will compile and print "i=1 j=1"

The code will compile and print "i=0 j=0"

2. Which statement is a syntactically correct way to declare an Array? Mark for Review
(1) Points

int i[1];

int[5] id={1,2,3,4,5};

int i[1] id;

int id[]; (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.


3. Using the code below, what will be the output if a Student object is instantiated?

public class Student


{
public Student()
{
System.out.print("1");
super();
System.out.print("2");
}
} Mark for Review
(1) Points

The code will not compile. (*)

12

Correct Correct

4. What is the final value of result from the following code snippet?
int i = 1;
int [] id = new int [3];
int result = id [i];
result = result + i; Mark for Review
(1) Points

The code will compile, result has the value of 0

The code will compile, result has the value of 2

The code will not compile, result has the value of 2

The code will compile, result has the value of 1 (*)

An exception is thrown.

Correct Correct

5. What is the output from the following code?


System.out.print("i=");
for (int i=0; i < l0; i++){
if(i==3)
break;
System.out.print(i);
} Mark for Review
(1) Points

i=0123456789

i=012 (*)

i=0123
i=012456789

Incorrect Incorrect. Refer to Section 4 Lesson 1.

6. Examine the following Classes:


Student and TestStudent
What is the output from the println statement in TestStudent?

public class Student {


private int studentId = 0;

public Student(){
studentId++;
}
public static int getStudentId(){
return studentId;
}
}

public class TestStudent {


public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
System.out.println(Student.getStudentId());
}
} Mark for Review
(1) Points

TestStudent will throw an exception

No output. Compilation of TestStudent fails (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

7. Which of following relationships does not use inheritance? Mark for Review
(1) Points

Car and Tire (*)

People and Student

Bank card and Credit Card

Animal and Cat

Correct Correct

8. Which statements are true?(Choose Three) Mark for Review


(1) Points

(Choose all correct answers)


Since a constructor can not return any value, it should be declared as void.

You can use access modifiers to control which other classes can call the constructor. (*)

You can declare more than one constructor in a class declaration. (*)

A constructor can not be overloaded.

In a constructor, you can call a superclass constructor. (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

9. What is the output from the following code snippet?

int i=0,j=0;
i=++i;
j=i++;
System.out.println("i=" + i + " " + "j=" + j); Mark for Review
(1) Points

The code will compile and print "i=1 j=2"

The code will compile and print "i=2 j=2"

The code does not compile.

The code will compile and print "i=1 j=1"

The code will compile and print "i=2 j=1" (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

10. Which two statements best describe data encapsulation? (Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

The access modifier to member data is private. (*)

Member data can be modified directly.

Methods provide for access and modification of data. (*)

The access modifier for methods is protected.

Incorrect Incorrect. Refer to Section 4 Lesson 1.

11. What is the output from the following code?


int x=0;
int y=5;
do{
++x;
y--;
}while(x<3);
System.out.println(x + " " + y); Mark for Review
(1) Points

22

3 2 (*)

23

33

Incorrect Incorrect. Refer to Section 4 Lesson 1.

12. The following code can be compiled, True/False?


byte b = 1;
b = b + 1; Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

13. What is the output from the following code snippet?

int[] myarray={1,2,3,4,5};
int sum=0;
for (int x : myarray)
sum+=x;
System.out.println("sum= " + sum); Mark for Review
(1) Points

10

The code will not compile.

20

15 (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

14. Which of following statments are true when you create a object from class Object at
runtime as seen below. (Choose Three)

java.lang.Object obj = new java.lang.Object(); Mark for Review


(1) Points

(Choose all correct answers)


Memory is allocated for a new object, if available. (*)

Reference obj can be reassigned to any other type of object. (*)

This Object instance will not be created because you never defined class Object.

A new instance of class Object is created. (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

15. What is the output from the following code snippet?


public class Test {
public static void main(String[] args) {
System.out.println(1 + 2 + "java" + 3);
}
} Mark for Review
(1) Points

The code does not compile.

The code will compile and print "12java3"

The code will compile and print "6java"

The code will compile and print "3java3" (*)

The code will compile and print "java3"


Incorrect Incorrect. Refer to Section 4 Lesson 1.

1. Which of following relationships does not use inheritance? Mark for Review
(1) Points

People and Student

Animal and Cat

Bank card and Credit Card

Car and Tire (*)

Correct Correct

2. What is the output from the following code?


int x=0;
int y=5;
do{
++x;
y--;
}while(x<3);
System.out.println(x + " " + y); Mark for Review
(1) Points

3 2 (*)
33

23

22

Incorrect Incorrect. Refer to Section 4 Lesson 1.

3. Which statement is incorrect regarding a constructor? Mark for Review


(1) Points

When no constructor is defined in the class, the compiler will create a default constructor.

The default constructor initializes the instance variables.

A constructor can not return value, it must be declared as void. (*)

The default constructor is the no-parameter constructor.

Correct Correct

4. What is the output from the following code snippet?


for (int i = 0; i < 10; i++) {
if (i == 3) {
break;
}
System.out.print(i); Mark for Review
(1) Points

The code will compile and print "123"

The code does not compile.

The code will compile and print "0123"

The code will compile and print "012" (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

5. A class can be extended by more than one class. True or False? Mark for Review
(1) Points

True (*)

False

Correct Correct

6. What is the output from the following code snippet?


String str1 = "java";
char[] c = {'j', 'a', 'v', 'a'};
System.out.println(str1.equals(c));
System.out.println(t == c); Mark for Review
(1) Points

The code will compile and print "true true"

The code will compile and print "false, true"

The code does not compile. (*)

The code will compile and print "false,false"

The code will compile and print "true, false"

Incorrect Incorrect. Refer to Section 4 Lesson 1.

7. What is the output from the following code snippet?

int i=0,j=0;
i=++i;
j=i++;
System.out.println("i=" + i + " " + "j=" + j); Mark for Review
(1) Points

The code will compile and print "i=2 j=2"

The code will compile and print "i=2 j=1" (*)

The code will compile and print "i=1 j=2"

The code does not compile.

The code will compile and print "i=1 j=1"

Incorrect Incorrect. Refer to Section 4 Lesson 1.

8. Which combination of the following overload the Student constructor?(Choose Two) Mark
for Review
(1) Points

(Choose all correct answers)

public Object Student(int x,int y){}

public Student(){} (*)

protected int Student(){}

public void Student(int x, int y){}

public Student(int x,int y){} (*)


Incorrect Incorrect. Refer to Section 4 Lesson 1.

9. Which two statements are access modifier keywords in Java?(Choose Two) Mark for
Review
(1) Points

(Choose all correct answers)

abstract

final

protected (*)

public (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

10. What is the output from the following code snippet?


String str1 = "java";
String str2 = "java";
System.out.println(str1.equals(str2) + "," + str1.equals(new String("hello"))); Mark for Review
(1) Points

The code will not compile.

The code will compile and print "true, false" (*)

The code will compile and print "false, true"

The code will compile and print "false, false"

The code will compile and print "true, true"

Incorrect Incorrect. Refer to Section 4 Lesson 1.

11. When you instantiate a subclass, the superclass constructor will be also invoked. True or
False? Mark for Review
(1) Points

True (*)

False

Correct Correct

12. Which statements are true?(Choose Three) Mark for Review


(1) Points

(Choose all correct answers)

You can use access modifiers to control which other classes can call the constructor. (*)
Since a constructor can not return any value, it should be declared as void.

You can declare more than one constructor in a class declaration. (*)

A constructor can not be overloaded.

In a constructor, you can call a superclass constructor. (*)

Correct Correct

13. Which ofthe following declarations are wrong?(Choose Three) Mark for Review
(1) Points

(Choose all correct answers)

abstract final class Hello{} (*)

public abstract class Student{}

protected private int id; (*)

abstract private void act(){} (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

14. Which statement is true when run the following statement?

1. String str = null;


2. if ((str != null) && (str.length() > 1)) {
3. System.out.println("great that number 1");
4. } else if ((str != null) & (str.length() < 2)) {
5. System.out.println("less than number 2");
6. } Mark for Review
(1) Points

The code compiles and will throw an exception at line 2.

The code compiles and will throw an exception at line 3.

The code compiles and will throw an exception at line 4. (*)

The code compiles and will throw an exception at line 5

The code does not compile.

Correct Correct

15. What is the output from the following code snippet?

int x = 1;
int y;
while(++x < 5)
y++;
System.out.println(y); Mark for Review
(1) Points

The code will not compile. (*)

Incorrect Incorrect. Refer to Section 4 Lesson 1.

Section 5
(Answer all questions in this section)

1. Immutable classes do allow instance variables to be changed by overriding methods.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.

2. An interface can implement methods.


True or False? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.

3. Immutable classes can be subclassed.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.

4. Unit testing can help you isolate problem quickly. True or False? Mark for Review
(1) Points
True (*)

False

Correct Correct

5. The main purpose of unit testing is to verify that an individual unit (a class, in Java) is
working correctly before it is combined with other components in the system. True or false?
Mark for Review
(1) Points

True (*)

False

Incorrect Incorrect. Refer to Section 5 Lesson 4


6. Which statement is true for the class java.util.ArrayList? Mark for Review
(1) Points

The elements in the collection are accessed using key.

The elements in the collection are ordered. (*)

The elements in the collection are immutable.

The elements in the collection are synchronized.

Incorrect Incorrect. Refer to Section 5 Lesson 4.

7. Which of the following is the correct way to throw cumstom ServerException? Mark for
Review
(1) Points

throw ServerException

throws ServerException

throw new ServerException() (*)

raise ServerException

Correct Correct

8. What symbol(s) is used to separate multiple exceptions in one catch statement? Mark for
Review
(1) Points

A single vertical bar | (*)

&&
(==) (equals equals)

None, multiple exceptions can't be handled in one catch statement.

Incorrect Incorrect. Refer to Section 5 Lesson 3.

9. What is the output from the following code snippet?

class Shape{
public void paint(){System.out.print("Shape");}

class Circle extends Shape{


public void paint() throws Exception{
System.out.print("Circle ");
throw new Exception();
}
public static void main(String[] args){
try{new Circle().paint();}
catch(Exception e){System.out.println("Exception");
}
} Mark for Review
(1) Points

Circle

Shape

ShapeCircle

Exception

Compile fails (*)

Incorrect Incorrect. Refer to Section 5 Lesson 3.

10. Assertions are boolean statements to test and debug your programs.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct
11. Which three types of objects can be thrown using a throw statement? (Choose Three) Mark
for Review
(1) Points

(Choose all correct answers)

Error (*)

Event
Exception (*)

Throwable (*)

Object

Incorrect Incorrect. Refer to Section 5 Lesson 3.

12. The instanceof operator can find subclass objects when they are passed to method which
declare a superclass type parameter.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

13. The instanceof operator allows you to determine the type of an object.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

14. Which one of the following would allow you to define the abstract class Animal. Mark for
Review
(1) Points

public abstract Animal {}

public Animal{}

public abstract Animal extends class{}

public abstract class Animal{} (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

15. An abstract class can implement its methods.


True or false? Mark for Review
(1) Points

True (*)
False
1. Immutable classes can be subclassed.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

2. Which one of the following would allow you to define an interface for Animal? Mark for
Review
(1) Points

public class Animal {}

public Animal extends Interface {}

public class Animal implements Interface {}

public interface Animal {} (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.

3. Immutable classes do allow instance variables to be changed by overriding methods.


True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

4. The instanceof operator can find subclass objects when they are passed to method which
declare a superclass type parameter.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

5. Which method will force a subclass to implement it? Mark for Review
(1) Points

Abstract public void act(); (*)


Public double act();

Protected void act(String name){}

Static void act(String name) {}

Public native double act();

Correct Correct

Section 5
(Answer all questions in this section)

1. What do Arrays and ArrayLists have in common?


I. They both store data.
II. They can both be traversed in loops.
III. They both can be dynamically re-sized during execution of a program.
Mark for Review
(1) Points

I only

II only

I and II only (*)

I, II and III only

None of these

Incorrect Incorrect. Refer to Section 5 Lesson 4.

2. Reading great code is just as important for a programmer as reading great books is for a
writer. True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

3. Unit testing can help you isolate problem quickly. True or False? Mark for Review
(1) Points

True (*)

False

Correct Correct
4. What is the result from creating the following try-catch block?

1.try {

2.} catch (Exception e) {

3.} catch (ArithmeticException a) {

4.} Mark for Review


(1) Points

Compile fails at Line 3 (*)

Compile fails at Line 2

The code compiles

Compile fails at Line 1

Incorrect Incorrect. Refer to Section 5 Lesson 3.

5. What is one step you must do to create your own exception? Mark for Review
(1) Points

Create a new class that implements Exception.

Create a new class that extends Exception. (*)

Exceptions cannot be created. They are only built in to Java.

Declare the primitive data type Exception.

Correct Correct
. Which statement added at line one allows the code to compile and run?

//line one
public class Test (
public static void main (String[] args) {
java.io.PrintWriter out = new java.io.PrintWriter
(new java.io.OutputStreamWriter (System.out), true);
System.out.println("Java");
}
} Mark for Review
(1) Points

No statement is needed. (*)

import java.io.OutputStreamWriter

include java.io.*;

import java.io.PrintWriter;

import java.io.*;
Incorrect Incorrect. Refer to Section 5 Lesson 3.

7. This is correct syntax for catching an exception:


try(inputStream = "missingfile.txt");
catch(exception e);
True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 3.

8. What is exception handling? Mark for Review


(1) Points

If your program exits before you expect it to.

When a file fails to open.

An error that occurs against the flow of your program.

A consistent way of handling various errors. (*)

Incorrect Incorrect. Refer to Section 5 Lesson 3.

9. Virtual method invocation requires that the superclass method is defined as which of the
following? Mark for Review
(1) Points

A private final method.

A default final method.

A public final method.

A public static method.

A public method. (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

10. An abstract class can implement its methods.


True or false? Mark for Review
(1) Points

True (*)
False

Correct Correct
11. Which line contains an compilation error?

interface Shape {}
interface InnerShape extends Shape{}
class Circle implements Shape{ }
class InnerCircle extends Circle{}
class Rectangle implements Shape{}
public class Tester {
public static void main(String[] args) {

Circle c= new Circle();


InnerCircle ic = new InnerCircle();
Rectangle rect = new Rectangle();

System.out.print(c instanceof InnerCircle); //Line 1


System.out.print(ic instanceof Circle); //Line 2
System.out.print(rect instanceof InnerCircle); //Line 3
System.out.print(rect instanceof Shape); //Line 4
}
} Mark for Review
(1) Points

Line 2

Line 3 (*)

Line 4

Line 1

Correct Correct

12. An abstract class can be instantiated.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

13. In general, classes can be made immutable by placing a final key word before the class
keyword.
True or false? Mark for Review
(1) Points

False

True (*)

Correct Correct
14. The state of an object differentiates it from other objects of the same class.
True or False? Mark for Review
(1) Points

True (*)

False

Correct Correct

15. Which one of the following would allow you to define an interface for Animal? Mark for
Review
(1) Points

public interface Animal {} (*)

public class Animal implements Interface {}

public class Animal {}

public Animal extends Interface {}

Correct Correct

ection 5
(Answer all questions in this section)

1. What is exception handling? Mark for Review


(1) Points

If your program exits before you expect it to.

A consistent way of handling various errors. (*)

When a file fails to open.

An error that occurs against the flow of your program.

Correct Correct

2. Which statements are true when you compile and run this code.(Choose Two)

1. public class Test{


2. public static String sayHello(String name) throws Exception{
3. if(name == null) throw new Exception();
4. return "Hello " + name;
5. }
6. public static void main(String[] args){
7. sayHello("Java");
8. }
9. } Mark for Review
(1) Points

(Choose all correct answers)


can not create Exception object in the Test Class.

The class Test compiles if line 6 contains a throws statement. public static void main(String[]
args) throws Exception{ (*)

The class Test compiles if line 7 is enclosed in a try-catch block. try{ sayHello("Java");
}catch(Exception e){} (*)

Compilation succeeds

Incorrect Incorrect. Refer to Section 5 Lesson 3.

3. Assertions are optional ways to catch logic errors in code.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

4. Assertions are boolean statements to test and debug your programs.


True or false? Mark for Review
(1) Points

True (*)

False

Incorrect Incorrect. Refer to Section 5 Lesson 3.

5. 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");
} Mark for Review
(1) Points

NullPointerException

Compile fails (*)

Java
Exception

Incorrect Incorrect. Refer to Section 5 Lesson 3.


6. A upward cast means all instance variables of the subclass are permanently lost to the
instance.
True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

7. The instanceof operator can find subclass objects when they are passed to method which
declare a superclass type parameter.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

8. A downward cast of a superclass to subclass allows you to access a subclass specialized


method call.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

9. You can't downcast an object explicitly because you must use virtual method invocation.
True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

10. Unit testing can help you isolate problem quickly. True or False? Mark for Review
(1) Points

True (*)
False

Correct Correct
11. 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.
Mark for Review
(1) Points

I and III only (*)

II and IV only

I, II, and III only

I, II, III and IV

None of these

Incorrect Incorrect. Refer to Section 5 Lesson 4.

12. Which statement is true for the class java.util.ArrayList? Mark for Review
(1) Points

The elements in the collection are synchronized.

The elements in the collection are accessed using key.

The elements in the collection are ordered. (*)

The elements in the collection are immutable.

Incorrect Incorrect. Refer to Section 5 Lesson 4.

13. A method with default access can be subclassed.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.

14. Which two statements are equivalent to line 2?


(Choose Two)
1. public interface Account{
2. int accountID=100;
3. } Mark for Review
(1) Points

(Choose all correct answers)

static int accountID=100; (*)

private int accountID=100;

Final int accountID=100; (*)

Abstract int accountID=100;

protected int accountID=100;

Incorrect Incorrect. Refer to Section 5 Lesson 2.

15. Classes define and implement what? Mark for Review


(1) Points

All methods with implementations

Variables and methods (*)

Some methods with implementations

All method definitions without any implementations

Constants and all methods with implementations


1. Virtual method invocation must be defined with the instanceof operator.
True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 1.

2. An abstract class can implement its methods.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

3. You can't downcast an object explicitly because you must use virtual method invocation.
True or false? Mark for Review
(1) Points
True

False (*)

Correct Correct

4. Which method will force a subclass to implement it? Mark for Review
(1) Points

Abstract public void act(); (*)

Protected void act(String name){}

Public double act();

Static void act(String name) {}

Public native double act();

Correct Correct

5. Methods can not throw exceptions.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 3.

6. In what order do multiple catch statements execute? Mark for Review


(1) Points

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.

Incorrect Incorrect. Refer to Section 5 Lesson 3.

7. Which statements are true when you compile and run this code.(Choose Two)

1. public class Test{


2. public static String sayHello(String name) throws Exception{
3. if(name == null) throw new Exception();
4. return "Hello " + name;
5. }
6. public static void main(String[] args){
7. sayHello("Java");
8. }
9. } Mark for Review
(1) Points

(Choose all correct answers)

can not create Exception object in the Test Class.

Compilation succeeds

The class Test compiles if line 6 contains a throws statement. public static void main(String[]
args) throws Exception{ (*)

The class Test compiles if line 7 is enclosed in a try-catch block. try{ sayHello("Java");
}catch(Exception e){} (*)

Correct Correct

8. This is correct syntax for catching an exception:


try(inputStream = "missingfile.txt");
catch(exception e);
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

9. Multiple exceptions can be caught in one catch statement.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

10. You can only implement one interface in a class.


True or False? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 5 Lesson 2.


11. Immutable classes can be subclassed.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

12. Which one of the following would allow you to define an interface for Animal? Mark for
Review
(1) Points

public class Animal {}

public Animal extends Interface {}

public class Animal implements Interface {}

public interface Animal {} (*)

Correct Correct

13. Which of the following statements is false? Mark for Review


(1) Points

An ArrayList has a fixed length. (*)

An ArrayList can store multiple object types.

An ArrayList can grow and shrink dynamically as required.

In an Array you need to know the length and the current number of elements stored.

Incorrect Incorrect. Refer to Section 5 Lesson 4.

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 1
(Answer all questions in this section)

1. The function of Garbage Collection in Java is: Mark for Review


(1) Points

The JVM uses GC to clear the program output.

The JVM GC deletes all unused Java files on the system.


Memory occupied by objects with no reference is automatically reclaimed for reuse. (*)

As a Java programmer, we have to call the GC function specifically in order to manage the Java
Memory.

Correct Correct

2. Which of the following allows the programmer to destroy an object referenced by x? Mark
for Review
(1) Points

x.remove();

x.finalize();

x.delete();

Only the garbage collection system can destroy an object. (*)

Correct Correct

3. Given the following output from the Minor GC:


[PSYoungGen: 9200K->1008K(9216K)] 9980K->3251K(19456K), 0.0045753 secs]
[Times:user=0.03 sys=0.03, real=0.00 secs]
Which of the following statements is TRUE? Mark for Review
(1) Points

The pause time spent in GC is 0.03.

The size of the entire heap is 19456k (*)

This is a major garbage collection process.

The size of the tenured space is 19456k.

Correct Correct

4. Java allows the same Java program to be executed on multiple operating systems. Mark for
Review
(1) Points

True (*)

False

Correct Correct

5. Which of the following statements describe the Java programming language? Mark for
Review
(1) Points
Java is a high-level programming language.

The Java programming language includes a garbage collection feature.

Java is an object oriented programming language.

All of the above (*)

Correct Correct

Page 1 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 1
(Answer all questions in this section)

6. Given the java snippet below:

public class Foo{


int x;
public void testFoo(){
int y=100;
}
}

Which of the following statements is TRUE? Mark for Review


(1) Points

Compile error, as the variable x is not initialized.

Variable x resides in the stack area, and variable y resides in the heap of the JVM.

Variable x resides in the heap area, and variable y resides in the stack of the JVM (*)

Variable x stays in the heap area, and variable y resides in the method area of the JVM.

Correct Correct

Section 2
(Answer all questions in this section)

7. The javac command can be used to display native code in Java Mark for Review
(1) Points

True

False (*)

Correct Correct
8. Which of the following commands can be used to translate Java source code into bytecode?
Mark for Review
(1) Points

java

javac (*)

jdb

jstat

Correct Correct

9. Which of the following statements is NOT TRUE for the jdb command? Mark for Review
(1) Points

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. (*)

Correct Correct

10. Like in the Java source code file, one Java class file can contain multiple class definitions.
Mark for Review
(1) Points

True

False (*)

Correct Correct

Previous Page 2 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 2
(Answer all questions in this section)

11. Which structure in the Java class file contains the line number information for the original
source file? Mark for Review
(1) Points

method_info (*)
this_class

filed_info

cp_info

Correct Correct

12. The attributes_count item indicates how many attributes are contained within a method.
Mark for Review
(1) Points

True (*)

False

Correct Correct

Section 3
(Answer all questions in this section)

13. Which of the following opcode instructions would add 2 integer variables? Mark for
Review
(1) Points

add

addi

iadd (*)

Incorrect Incorrect. Refer to Section 3 Lesson 1.

14. Choose which opcode is used to push an int constant 5 onto the operand stack. Mark for
Review
(1) Points

iconst_5 (*)

idc5

iload_5

iaload_5

iinc5
Correct Correct

15. opcode invokespecial is used to invoke an instance initialization method. Mark for Review
(1) Points

True (*)

False
16. Which of the following is NOT a java class loader? Mark for Review
(1) Points

verification class loader (*)

application class loader

bootstrap class loader

extension class loader

Correct Correct

17. The Java developer can define a number of additional or custom classloaders. Mark for
Review
(1) Points

True (*)

False

Correct Correct

18. Which of the following exceptions is thrown by the loadClass() method of ClassLoader
class? Mark for Review
(1) Points

IOException

SystemException

ClassFormatError

ClassNotFoundException (*)

Correct Correct

Section 4
(Answer all questions in this section)
19. Which three are valid declarations for a float value? (Choose Three) Mark for Review
(1) Points

(Choose all correct answers)

float f = -1; (*)

float f = 0x345; (*)

float f = 3.0L;

float f = 2.0f; (*)

float f = 1.0;

Correct Correct

20. What is the final value of result from the following code snippet?
int i = 1;
int [] id = new int [3];
int result = id [i];
result = result + i; Mark for Review
(1) Points

The code will compile, result has the value of 0

The code will compile, result has the value of 1 (*)

The code will not compile, result has the value of 2

The code will compile, result has the value of 2

An exception is thrown.

Correct Correct

Previous Page 4 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 4
(Answer all questions in this section)

21. What is the output from the following code snippet?


String str1= "java";
String str2=new String("java");
System.out.println( str1==str2 );
System.out.println( str1==str2.intern() );
Mark for Review
(1) Points

The code does not compile.


The code will compile and print "true false"

The code will compile and print "false true" (*)

The code will compile and print "false false"

The code will compile and print "true true"

Correct Correct

22. What is the output from the following code snippet?

int[] myarray={1,2,3,4,5};
int sum=0;
for (int x : myarray)
sum+=x;
System.out.println("sum= " + sum); Mark for Review
(1) Points

The code will not compile.

10

15 (*)

20

Correct Correct

23. Examine the following Classes:


Student and TestStudent
What is the output from the println statement in TestStudent?

public class Student {


private int studentId = 0;

public Student(){
studentId++;
}
public static int getStudentId(){
return studentId;
}
}

public class TestStudent {


public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
System.out.println(Student.getStudentId());
}
} Mark for Review
(1) Points

No output. Compilation of TestStudent fails (*)


TestStudent will throw an exception

Correct Correct

24. Which ofthe following declarations are wrong?(Choose Three) Mark for Review
(1) Points

(Choose all correct answers)

abstract private void act(){} (*)

public abstract class Student{}

protected private int id; (*)

abstract final class Hello{} (*)

Correct Correct

25. Which of the following types are primitive data types? (Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

String

boolean (*)

double (*)

Integer

Correct Correct

Previous Page 5 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 5
(Answer all questions in this section)

26. Immutable classes do allow instance variables to be changed by overriding methods.


True or false? Mark for Review
(1) Points

True
False (*)

Correct Correct

27. You can only implement one interface in a class.


True or False? Mark for Review
(1) Points

True

False (*)

Correct Correct

28. Interfaces define what? Mark for Review


(1) Points

Constants and all methods with implementations

Some methods with implementations

All method definitions without any implementations (*)

All methods with implementations

Variables and methods

Correct Correct

29. Modeling classes for a business problem requires understanding of the business not Java.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

30. Classes define and implement what? Mark for Review


(1) Points

Some methods with implementations

Constants and all methods with implementations

All methods with implementations


All method definitions without any implementations

Variables and methods (*)

Correct Correct

Previous Page 6 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 5
(Answer all questions in this section)

31. An interface can implement methods.


True or False? Mark for Review
(1) Points

True

False (*)

Correct Correct

32. When line 10 is executed, which method will be called?


1 class Account {
2 public void deposit(int amt, int amt1) { }
3 public void deposit(int amt){ }
4}
5 public class CreditAccount extends Account {
6 public void deposit() { }
7 public void deposit(int amt) {}
8 public static void main(String args[]){
9 Account account = new CreditAccount();
10 account.deposit(10);
11 }
12 } Mark for Review
(1) Points

line 7 (*)

line 2

line 3

line 6

Correct Correct

33. You can always upcast a subclass to an interface provided you don't need to access any
members of the concrete class.
True or false? Mark for Review
(1) Points
True (*)

False

Correct Correct

34. Which two of the following statements are true? (Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

An abstract class must contain abstrct method.

An abstract class can define constructor. (*)

An abstract class can create subclass and be constructed.

An abstract class must be difined by using the abstract keyword. (*)

Correct Correct

35. An abstract class can implement its methods.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

Previous Page 7 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 5
(Answer all questions in this section)

36. Virtual method invocation must be defined with the instanceof operator.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

37. You can't downcast an object explicitly because you must use virtual method invocation.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

38. Which of the following is the correct way to throw cumstom ServerException? Mark for
Review
(1) Points

throw ServerException

throws ServerException

throw new ServerException() (*)

raise ServerException

Correct Correct

39. When will a finally statement be executed? Mark for Review


(1) Points

Always; no matter if an exception is thrown or not. (*)

Only if an exception is not thrown.

Never; it is there for visual purposes.

Only if multiple exceptions are caught and thrown.

Only if an exception is thrown.

Incorrect Incorrect. Refer to Section 5 Lesson 3.

40. 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");
} Mark for Review
(1) Points

Exception
NullPointerException

Compile fails (*)

Java

Correct Correct

Previous Page 8 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 5
(Answer all questions in this section)

41. Which statements are true when you compile and run this code.(Choose Two)

1. public class Test{


2. public static String sayHello(String name) throws Exception{
3. if(name == null) throw new Exception();
4. return "Hello " + name;
5. }
6. public static void main(String[] args){
7. sayHello("Java");
8. }
9. } Mark for Review
(1) Points

(Choose all correct answers)

The class Test compiles if line 7 is enclosed in a try-catch block. try{ sayHello("Java");
}catch(Exception e){} (*)

Compilation succeeds

can not create Exception object in the Test Class.

The class Test compiles if line 6 contains a throws statement. public static void main(String[]
args) throws Exception{ (*)

Correct Correct

42. The finally clause only executes when an exception is not caught and thrown.
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct
43. In what order do multiple catch statements execute? Mark for Review
(1) Points

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.

Correct Correct

44. Methods can not throw exceptions.


True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

45. What do Arrays and ArrayLists have in common?


I. They both store data.
II. They can both be traversed in loops.
III. They both can be dynamically re-sized during execution of a program.
Mark for Review
(1) Points

I only

II only

I and II only (*)

I, II and III only

None of these

Correct Correct

Previous Page 9 of 10 Next Summary

Test: Java Programming Midterm Exam


Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct
answer.

Section 5
(Answer all questions in this section)
46. The main purpose of unit testing is to verify that an individual unit (a class, in Java) is
working correctly before it is combined with other components in the system. True or false?
Mark for Review
(1) Points

True (*)

False

Correct Correct

47. Why is it helpful for new programmers to read pre-written code?(Choose Two) Mark for
Review
(1) Points

(Choose all correct answers)

It is not helpful to read code written by other programmers.

Learn new programming techniques. (*)

Understand code written by other programmers. (*)

Meet new programmers.

Correct Correct

48. Which of the following statements is false? Mark for Review


(1) Points

An ArrayList has a fixed length. (*)

An ArrayList can store multiple object types.

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.

Correct Correct

49. Which of the following statements about unit testing is/are true
I. When all unit tests succeed, you can have high confidence your code is solid.
II. If a unit test fails, you donメt proceed until the code is fixed and the test succeeds.
III. Unit testing can help developer find problems early in the development cycle
Mark for Review
(1) Points

I only

I and II only

II and III only (*)


I, II, and III

None of these

Correct Correct

50. 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.
Mark for Review
(1) Points

I and III only (*)

II and IV only

I, II, and III only

I, II, III and IV

None of these
14. Why is it helpful for new programmers to read pre-written code?(Choose Two) Mark for
Review
(1) Points

(Choose all correct answers)

Learn new programming techniques. (*)

Meet new programmers.

Understand code written by other programmers. (*)

It is not helpful to read code written by other programmers.

Incorrect Incorrect. Refer to Section 5 Lesson 4.

15. Which of the following statements about inheritance is false? Mark for Review
(1) Points

A subclass inherits all the members (fields, methods, and nested classes) from its superclass.

Inheritance allows you to reuse the fields and methods of the super class without having to write
them yourself.

Inheritance allows you to minimize the amount of duplicate code in an application by sharing
common code among several subclasses.

Through inheritance, a parent class is a more specialized form of the child class. (*)
Incorrect Incorrect. Refer to Section 5 Lesson 4.

Section 6
(Answer all questions in this section)

1. A LinkedList is a type of Stack.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

2. Which of the following is a list of elements that have a first in last out ordering. Mark for
Review
(1) Points

Arrays

HashMaps

Enums

Stacks (*)

Incorrect Incorrect. Refer to Section 6 Lesson 3.

3. A HashMap can only store String types.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 6 Lesson 3.

4. Which of the following correctly defines a queue? Mark for Review


(1) Points

A list of elements with a first in last out order.

A list of elements with a first in first out order. (*)

It is a keyword in Java that restrict the use of the code to local users only.

Something that enables you to create a generic class without specifying a type between angle
brackets <>.
Incorrect Incorrect. Refer to Section 6 Lesson 3.

5. Which line contains an compilation error?

interface Shape {}
class Circle implements Shape{}

public class Test{


public static void main(String[] args) {
List ls = new ArrayList(); // Line 1
List lc = new ArrayList(); // Line 2
Circle c = new Circle();
ls.add(c); // Line 3
lc.add(c); // Line 4
}
} Mark for Review
(1) Points

Line 2

Line 4

Line 1

Line 3 (*)

Incorrect Incorrect. Refer to Section 6 Lesson 2.


6. Which class is an ordered collection that may contain duplicates? Mark for Review
(1) Points

list (*)

enum

set

array

Incorrect Incorrect. Refer to Section 6 Lesson 2.

7. The following code is valid when working with the Collection Interface.
Collection collection = new Collection()d;
True or false? Mark for Review
(1) Points

True

False (*)

Correct Correct

8. What is the output from the following code snippet?


public static void main(String[] args){

List li=new ArrayList();


li.add(1);
li.add(2);
print(li);
}
public static void print(List list) {
for (Number n : list)
System.out.print(n + " ");
} Mark for Review
(1) Points

The code will not compile.

1 2 (*)

Incorrect Incorrect. Refer to Section 6 Lesson 1.

9. < ? extends Animal > is an example of a bounded generic wildcard.


True or False? Mark for Review
(1) Points

True (*)

False

Correct Correct

10. What is the correct definition of Enumeration (or enum)? Mark for Review
(1) Points

Code that initializes an ArrayList

A list of elements that is dynamically stored.

A bounded generic class

A keyword that specifies a class whose objects are defined inside the class. (*)

Incorrect Incorrect. Refer to Section 6 Lesson 1.


11. Which of the following would initialize a generic class "Cell" using a String type?
I. Cell cell = new Cell(); .
II. Cell cell = new Cell(); .
III. Cell cell = new String;
Mark for Review
(1) Points

I and II (*)
II only

II and III

I only

III only

Incorrect Incorrect. Refer to Section 6 Lesson 1.

12. Why might a sequential search be inefficient? Mark for Review


(1) Points

It utilizes the "divide and conquer" method, which makes the algorithm more error prone.

It requires incrementing through the entire array in the worst case, which is inefficient on large
data sets. (*)

It involves looping through the array multiple times before finding the value, which is inefficient
on large data sets.

It is never inefficient.

Incorrect Incorrect. Refer to Section 6 Lesson 4.

13. Which of the following sorting algorithms utilizes a "divide and conquer" technique to sort
arrays with optimal speed? Mark for Review
(1) Points

Sequential Search

Merge Sort (*)

Selection Sort

Binary Search

All of the above

Incorrect Incorrect. Refer to Section 6 Lesson 4.

14. Bubble Sort is a sorting algorithm that involves swapping the smallest value into the first
index, finding the next smallest value and swapping it into the next index and so on until the
array is sorted.
True or false? Mark for Review
(1) Points

True

False (*)
Incorrect Incorrect. Refer to Section 6 Lesson 4.

15. A sequential search is an iteration through the array that stops at the index where the
desired element is found.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

Section 6
(Answer all questions in this section)

1. Which line contains an compilation error?

interface Shape {}
class Circle implements Shape{}

public class Test{


public static void main(String[] args) {
List ls = new ArrayList(); // Line 1
List lc = new ArrayList(); // Line 2
Circle c = new Circle();
ls.add(c); // Line 3
lc.add(c); // Line 4
}
} Mark for Review
(1) Points

Line 3 (*)

Line 4

Line 1

Line 2

Correct Correct

2. Which of these could be a set? Why? Mark for Review


(1) Points

{1, 1, 2, 22, 305, 26} because a set may contain duplicates and all its elements are of the same
type.

{"Apple", 1, "Carrot", 2} because it records the index of the elements with following integers.

{1, 2, 5, 178, 259} because it contains no duplicates and all its elements are of the same type.
(*)

All of the above are sets because they are collections that can be made to fit any of the choices.
Incorrect Incorrect. Refer to Section 6 Lesson 2.

3. Which code inserted into the code below guarantees that the program will output [1,2]?
import java.util.*;
public class Example{
public static void main(String[] args){
//insert code here
set.add(2);
set.add(1);
System.out.println(set);
}
} Mark for Review
(1) Points

Set set = new SortedSet();

Set set = new TreeSet(); (*)

Set set = new LinkedHashSet();

Set set = new HashSet();

List set = new SortedList();

Incorrect Incorrect. Refer to Section 6 Lesson 2.

4. Big-O Notation is used in Computer Science to describe the performance of Sorts and
Searches on arrays. True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

5. Selection sort is efficient for large arrays.


True or false? Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 6 Lesson 4.

6. Which of the following sorting algorithms utilizes a "divide and conquer" technique to sort
arrays with optimal speed? Mark for Review
(1) Points

Sequential Search

Merge Sort (*)


Selection Sort

Binary Search

All of the above

Incorrect Incorrect. Refer to Section 6 Lesson 4.

7. A sequential search is an iteration through the array that stops at the index where the
desired element is found.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

8. Enumerations (enums) are useful for storing data : Mark for Review
(1) Points

When you know all of the possibilities of the class (*)

When the class is constantly changing

When the class is a subclass of Object.

You cannot store data in an enum.

Incorrect Incorrect. Refer to Section 6 Lesson 1.

9. A generic class increases the risk of runtime class conversion exceptions.


True or False? Mark for Review
(1) Points

True

False (*)

Correct Correct

10. Generic methods are required to be declared as static.


True or False? Mark for Review
(1) Points

True
False (*)

Correct Correct

11. The following code will compile.


True or False?

class Node implements Comparable{


public int compareTo(T obj){return 1;}
}

class Test{
public static void main(String[] args){
Node nc=new Node<>();
Comparable com=nc;
}
Mark for Review
(1) Points

True (*)

False

Correct Correct

12. Which scenario best describes a stack? Mark for Review


(1) Points

A pile of pancakes with which you add some to the top and remove them one by one from the
top to the bottom. (*)

A row of books that you can take out of only the middle of the books first and work your way
outward toward either edge.

A line at the grocery store where the first person in the line is the first person to leave.

All of the above describe a stack.

Incorrect Incorrect. Refer to Section 6 Lesson 3.

13. Where you enqueue an element in the list? Mark for Review
(1) Points

adds it to the end of the list (*)

adds it to the start of the list

removes it from the front of the list

removes it from the end of the list

Incorrect Incorrect. Refer to Section 6 Lesson 3.


14. A LinkedList is a type of Stack.
True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

15. A LinkedList is a list of elements that is dynamically stored.


True or false? Mark for Review
(1) Points

True (*)

False

Correct Correct

1. All classes can by subclassed. Mark for Review


(1) Points

True

False (*)

5. What is special about including a resource in a try statement?(Choose Two) Mark for Review
(1) Points

(Choose all correct answers)

The resources will auto-close. (*)

An error will be thrown if the resources does not open. (*)

The program will fail if the resource does not open

6. Why should you not use assertions to check parameters? Mark for Review
(1) Points

Assertions can be disabled at run time which may cause unexpected results in your assertions.
(*)

Not all methods have parameters, therefore assertions should never be used on parameters.

It is hard to assume expected values for parameters.

Assertions do not work on parameters

15. When an object is able to pass on its state and behaviors to its children, this is called: Mark
for Review
(1) Points
Inheritance (*)

Encapsulation

Polymorphism

Isolation

Posted by karre resakl at 7:27 PM 58 comments:

Home

Subscribe to: Posts (Atom)

Picture Window theme. Theme images by Michael Elkan. Powered by Blogger.

Вам также может понравиться