wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Final exam (comprog2)

Total questions: 114

Worksheet time: 19mins

Name
Class
Date
1.

A while loop carries on as long as...

a)

The condition in the brackets is true

b)

The condition in the brackets is false

c)

i < 10

d)

the for loop has not ended

2.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

3.

OUTPUT?

int i = 1;

while (i < 5) {

System.out.println("Hello");

i++;

}

a)

Hello

Hello

Hello

Hello

Hello

b)

Hello

Hello

Hello

Hello

c)

Hello

Hello

Hello

d)

Hello

4.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

5.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

6.

How many times does the loop print a *

a)

40

b)

20

c)

24

d)

30

e)

Infinite Loop

7.

What does the following code print?

a)

A rectangle of 8 rows with 5 stars per row

b)

A rectangle of 8 rows with 4 stars per row

c)

A rectangle of 6 rows with 5 stars per row

d)

A rectangle of 6 rows with 4 stars per row

e)

Infinite Looo

8.

What does the following code print?

a)

A rectangle of 9 rows with 5 stars per row

b)

A rectangle of 6 rows with 6 stars per row

c)

A rectangle of 7 rows with 5 stars per row

d)

A rectangle of 7 rows with 6 stars per row

e)

Infinite Loop

9.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

10.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

11.
a)

1 2 4 8 16 32 64

b)

1 2 4 8 16 32

c)

2 4 8 16 32 64

d)

2 4 8 16 32

e)

4 8 16 32 64

12.
a)

200 66 22 7 2

b)

66 22 7 2

c)

200 66 22 2

d)

200 66 22

e)

7

13.
a)

10

b)

15

c)

17

d)

22

e)

25

14.

2

a)

5

b)

7

c)

9

d)

13

e)

20

15.
a)

-1 0 2 5

b)

-1 0 3 7

c)

-1 1 4 8

d)

0 2 5 9

e)

0 1 3 7

16.
a)

0.0

b)

-4.0

c)

5

d)

The program will end successfully, but nothing will be printed.

e)

Nothing will be printed. Run-time error: ArithmeticException.

17.

What will the following program segment display?


for(int a = 2; a <= 10; a += 3)

System.out.print(a+””);

a)

2 5 8

b)

2 5

c)

2 5 8 11

d)

2

18.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
19.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
20.
Boolean values are: 
a)
True / False
b)
Yes / No
c)
Right / Wrong
21.

_________________ is a collection of elements used to store the same type of data.

a)

Array

b)

Switch

c)

Case

d)

Loop

22.

What is returned by values[5]?

a)

2

b)

12

c)

6

d)

8

23.

var nums =[2, 3, 5, 8, 9, 11];

How would you access the fourth element in nums

a)

nums[4]

b)

nums[3]

c)

nums(4)

d)

nums(3)

24.

X is ?

a)

Array

b)

Variable

25.

X is ?

a)

Array

b)

variable

26.

............. is a placeholder for only one data?

a)

Array

b)

Variable

27.

.......... is a placeholder for related data?

a)

Array

b)

variable

28.

which option represent: Array holding a String data?

a)
b)
29.

var nums =[2, 3, 5, 8, 9, 11];

How would you access the first element in nums

a)

num(1);

b)

num[1];

c)

num(0);

d)

num[0];

30.

x is array of 10 items, to access the last index ?

a)

x[10];

b)

x(10);

c)

x[9];

d)

x(9);

31.

using (appendItem) will add the new item to the ........ of the array

a)

First

b)

Middle

c)

End

32.

Entire array is enclosed in:

a)

( )

b)

{ }

c)

[ ]

33.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

34.

OUTPUT?

int i = 1;

while (i < 5) {

System.out.println("Hello");

i++;

}

a)

Hello

Hello

Hello

Hello

Hello

b)

Hello

Hello

Hello

Hello

c)

Hello

Hello

Hello

d)

Hello

35.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

36.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

37.

How many times does the loop print a *

a)

40

b)

20

c)

24

d)

30

e)

Infinite Loop

38.

What does the following code print?

a)

A rectangle of 8 rows with 5 stars per row

b)

A rectangle of 8 rows with 4 stars per row

c)

A rectangle of 6 rows with 5 stars per row

d)

A rectangle of 6 rows with 4 stars per row

e)

Infinite Looo

39.

What does the following code print?

a)

A rectangle of 9 rows with 5 stars per row

b)

A rectangle of 6 rows with 6 stars per row

c)

A rectangle of 7 rows with 5 stars per row

d)

A rectangle of 7 rows with 6 stars per row

e)

Infinite Loop

40.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

41.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

42.
In the statement  int x; What is x ?
a)
a variable
b)
a data type
c)
a class
d)
an object
43.
In the statement 
 Bank tom = new Bank(5000.0);
Which is the class?
a)
Bank
b)
tom
c)
new
d)
5000.0
44.
In the statement 
Bank tom = new Bank(5000.0);

Which is the object?
a)
Bank
b)
tom
c)
new
d)
5000.0
45.
In the statement  int x; What is int ?
a)
a variable
b)
a data type
c)
a class
d)
an object
46.
How many objects can be made from a class?  
a)
None, you make classes from objects
b)
one
c)
As many as you want
d)
All of the above
47.
The ____ method executes first in an application, regardless of where you physically place it within its class.
a)
execute()
b)
main()
c)
start()
d)
run()
48.

which package is imported implicitly?

a)

java.applet

b)

java.util

c)

java.lang

d)

java.io

49.

Classes in Java are:

a)

Abstract data types, used as a template of objects

b)

Where students learn

c)

Dungeons and Dragons Character types

d)

None of these

50.

Which is the fundamental unit of OOP?

a)

Object

b)

Class

51.

Class is a_________.

a)

object / Template

b)

blue print / Template

c)

Instance / blue print

52.

<class name><Object name>=new <class name>();

This is syntax of creating ______.

a)

class

b)

object

c)

method

d)

function

53.

TextBook English=new Textbook();

In the above snippet, TextBook is a _______.

a)

Object

b)

Data member

c)

Member function

d)

Class

54.

TextBook English=new Textbook();

In the above snippet, English is an _______.

a)

Object

b)

Class

c)

Member function

d)

Class name

55.

The process of binding data and functions is called as

a)

Inheritance

b)

Polymorphism

c)

Abstraction

d)

Encapsulation

56.

The process of hiding the implementation details from the user is called

a)

Abstraction

b)

Polymorphism

c)

Inheritance

d)

Encapsulation

57.

A class can have any number of objects

a)

false

b)

true

c)

neither true nor false

d)

cannot say

58.

The OOPs concept, exposing only necessary information to users or clients is known as

a)

Inheritance

b)

Insulation

c)

Abstraction

d)

Polymorphism

59.

Predict the output of following Java program?

class Test {

int i;

}

class Main {

public static void main(String args[]) {

Test t;

System.out.println(t.i);

}

a)

0

b)

garbage value

c)

Compiler Error

d)

Run Time Error

60.

Predict the output of following Java program

class Test

{ int i;

}

class Main

{ public static void main(String args[])

{ Test t = new Test();

System.out.println(t.i);

} }

a)

Garbage Value

b)

0

c)

Compiler Error

d)

Runtime error

61.

A way to discover which of the two classes is the base class and which is the subclass is to _______.

a)

look at the class size

b)

try saying the two class names together

c)

use polymorphism

d)

Both a and b are correct

62.

Employing inheritance reduces errors because ______.

a)

the new classes have access to fewer data fields

b)

the new classes have access to fewer methods

c)

you can copy methods that you already created

d)

many of the methods you need have already been used and tested

63.

A base class can also be called a ______.

a)

child class

b)

subclass

c)

derived class

d)

superclass

64.

The Java keyword that creates inheritance is _____.

a)

static

b)

enlarge

c)

extends

d)

inherits

65.

A class named Building has a public, nonstatic method named getFloors(). If School is a child class of Building, and modelHigh is an object of type School, which of the following statements is valid?

a)

Building.getFloors();

b)

School.getFloors();

c)

modelHigh.getFloors();

d)

All of the previous statements are valid.

66.

Which of the following statements is true?

a)

A child class inherits from a parent class.

b)

A parent class inherits from a child class.

c)

Both of the preceding statements are true.

d)

Neither a nor b is true.

67.

How would you declare and initialize a button in Java?

(use button as the object name and with caption "Click me!") (5 points)

(a)  

68.

In Java, everything is an Object. Java can be easily extended since it is based on the Object model. Which characteristic of Java was described?

a)

Object-oriented

b)

Simple

c)

Secure

d)

Robust

69.

Which feature makes it possible to write programs that can perform many tasks simultaneously.

a)

Interpreted

b)

Multithreaded

c)

Efficient

d)

Multi-purpose

70.

On what year was Oak renamed as Java

a)

1998

b)

1999

c)

1996

d)

1995

71.

What is the name of the company that developed Java?

a)

Sun Microsystems

b)

IBM

c)

The Green Team

d)

Microsoft

72.

What is the file extension for Java Source Code?

a)

.java

b)

.class

c)

.exe

d)

.obj

73.

What is the old name for Java Programming?

a)

Java2

b)

Oak

c)

C++

d)

Greenteam

74.

Tools that provide support for software development process including editors and debuggers are called

a)

integrated development environments

b)

bytecode converters

c)

virtual machine converters

d)

memory verifier

75.

Which of the following is not a feature of Java

a)

Robust

b)

Platform Specific

c)

Multithreaded

d)

Object-Oriented

76.

From where did Java get its name?

a)

Coffee

b)

Book

c)

Initials of the developers of Java

d)

TV programme

77.

Who invented Java?

a)

Dennis Ritchie

b)

Ken Thompson

c)

Bjarne Stroustrup

d)

James Gosling

78.

This java version is meant to have large number of users

a)

JAVA EE

b)

JAVA SE

c)

JAVA ME

79.

Java is familiar because

a)

Memory allocation and Garbage Collection is taken care of

b)

Patterned from C++

c)

Components of the program are treated like objects

80.

A code written using any programming language that is translated into binary code is called

a)

Source code

b)

Bytecode

c)

Pseudocode

d)

Flowchart

81.

What is the file extension for Java Executable Code?

a)

.java

b)

.class

c)

.exe

d)

.obj

82.

It is a type of java program that can be embedded in a web.

a)

general purpose application

b)

standalone applications

c)

applet

d)

webpage

83.

Java programs ________________

a)

are only compiled

b)

are only compiled not interpreted

c)

are only interpreted

d)

are both compiled and interpreted

84.

What are the values of var1 and var2 when the code finishes executing?

a)

var1 = 1, var2 = 1

b)

var1 = 2 , var2 = 0

c)

var1 = 3, var2 = -1

d)

var1 = 0, var2 = 2

e)

The loop will cause a run-time error for division by zero

85.

What are the values of x and y when the code finishes executing?

a)

x = 5, y = 2

b)

x = 2, y = 5

c)

x = 3, y = 4

d)

x = 4, y = 3

e)

Infinite Loop

86.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

87.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

88.

How many times does the loop print a *

a)

40

b)

20

c)

24

d)

30

e)

Infinite Loop

89.

What does the following code print?

a)

A rectangle of 8 rows with 5 stars per row

b)

A rectangle of 8 rows with 4 stars per row

c)

A rectangle of 6 rows with 5 stars per row

d)

A rectangle of 6 rows with 4 stars per row

e)

Infinite Looo

90.

What does the following code print?

a)

A rectangle of 9 rows with 5 stars per row

b)

A rectangle of 6 rows with 6 stars per row

c)

A rectangle of 7 rows with 5 stars per row

d)

A rectangle of 7 rows with 6 stars per row

e)

Infinite Loop

91.

How many times is "HI!" printed?

a)

15

b)

12

c)

10

d)

8

e)

Infinite Loop

92.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

93.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

94.

What does the following code segment print?

a)

5 4 3 2 1

b)

-5 -4 -3 -2 -1

c)

-4 -3 -2 -1 0

95.

How many times does the code print a * ?

a)

7

b)

8

c)

12

d)

13

96.

How many times does this print a * ?

a)

4

b)

5

c)

6

d)

7

97.

How many times does this code print at * ?

a)

5

b)

6

c)

7

d)

8

98.

What does the code print?

a)

1 2 3 4

b)

1 2 3 4 5

c)

0 1 2 3 4

d)

0 1 2 3 4 5

99.

How many stars are printed?

a)

10

b)

5

c)

25

d)

50

100.

What is printed when the code segment is executed?

a)

0 2 4 6 8 10 12 14 16 18

b)

0 6 12 18

c)

1 4 7 10 13 16 19

d)

4 10 16

101.

What is polymorphism in Java?

a)

Polymorphism in Java is only applicable to variables

b)

Polymorphism in Java is the ability of a single method to perform different actions based on the object it is acting upon.

c)

Polymorphism in Java is a feature that allows static binding

d)

Polymorphism in Java is the ability to use multiple inheritance

102.

Explain the concept of method overriding in polymorphism.

a)

Method overriding is the ability of a subclass to provide a specific implementation of a method that is already provided by its parent class.

b)

Method overriding is the ability of a subclass to provide a completely new method that is not related to the parent class method.

c)

Method overriding is the ability of a subclass to inherit the method implementation from the parent class without any changes.

d)

Method overriding is the ability of a subclass to provide a general implementation of a method that is already provided by its parent class.

103.

How does polymorphism help in achieving dynamic method dispatch?

a)

Polymorphism enables superclass references to subclass objects, allowing for dynamic method dispatch.

b)

Polymorphism causes errors when trying to call methods from subclass objects.

c)

Polymorphism has no impact on method dispatch, it only affects variable binding.

d)

Polymorphism restricts superclass references to subclass objects, hindering dynamic method dispatch.

104.

What is the difference between compile-time polymorphism and runtime polymorphism?

a)

Compile-time polymorphism is resolved during program execution

b)

The difference lies in how the polymorphism is resolved - compile-time polymorphism is resolved during compilation, while runtime polymorphism is resolved during program execution.

c)

Runtime polymorphism is resolved during compilation

d)

Both compile-time and runtime polymorphism are resolved during compilation

105.

Give an example of polymorphism using method overloading.

a)

Using method overloading with only one method

b)

Implementing method overloading with different return types

c)

Having multiple 'calculate' methods with different parameter types such as int, double, and String.

d)

Having multiple 'calculate' methods with the same parameter type

106.

Discuss the importance of polymorphism in object-oriented programming.

a)

Polymorphism limits the functionality of OOP

b)

Polymorphism leads to code duplication in OOP

c)

Polymorphism is irrelevant in OOP

d)

Polymorphism is important in OOP for flexibility, reusability, and extensibility.

107.

Explain the use of the 'instanceof' operator in polymorphism.

a)

The 'instanceof' operator is used to check if an object is null in polymorphism.

b)

The 'instanceof' operator is used to determine the size of an object in polymorphism.

c)

The 'instanceof' operator is used to perform mathematical operations on objects in polymorphism.

d)

The 'instanceof' operator is used to determine if an object is an instance of a particular class or interface in polymorphism.

108.

What are the benefits of using polymorphism in Java?

a)

Polymorphism leads to slower performance in Java programs

b)

Polymorphism only works with primitive data types

c)

Polymorphism helps achieve flexibility and code reusability by allowing objects of different classes to be treated as objects of a common superclass.

d)

Polymorphism makes code harder to read and understand

109.

Provide a real-world example where polymorphism can be applied effectively.

a)

Software application managing different shapes

b)

Hardware device managing different sizes

c)

Software application managing different colors

d)

Online platform managing different animals

110.

What does the word “poly” in polymorphism mean?

a)

many

b)

one

c)

two

d)

three

111.

Java supports:

a)

run time polymorphism

b)

 compile time polymorphism

c)

both run time and compile time polymorphism

d)

 neither run time nor compile time polymorphism

112.

What does 'Simple' mean in the context of Java features?

a)

Complexity and difficulty in language design

b)

Simplicity and ease of use in language design

c)

Elaborate and intricate coding structures

d)

Advanced and convoluted programming concepts

113.

Why is Java considered 'Secured'?

a)

Java is not considered 'Secured' because it has many vulnerabilities.

b)

Java is considered 'Secured' due to its slow performance.

c)

Java has bytecode verification, classloader, and security manager features.

d)

Java is not considered 'Secured' because it lacks encryption capabilities.

114.

Explain the concept of 'Distributed' in Java.

a)

Java 'Distributed' concept enables programs to run on multiple networked computers.

b)

Java 'Distributed' concept allows programs to run on a single computer only.

c)

Java 'Distributed' concept is not related to networking.

d)

Java 'Distributed' concept is only applicable to mobile applications.