wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Programming Basics Assessment

Total questions: 50

Worksheet time: 40mins

Name
Class
Date
1.

What is the correct way to declare a variable in Java?

a)

int variableName;

b)

int: variableName;

c)

var variableName int;

d)

int variableName = 0;

2.

Which of the following is a valid data type in Java?

a)

int

b)

boolean

c)

string

d)

float

3.

What is the output of the following code: System.out.println(5 + 3);?

a)

8

b)

10

c)

9

d)

6

4.

Which keyword is used to create a class in Java?

a)

define

b)

class

c)

create

d)

function

5.

What is the default value of a boolean variable in Java?

a)

1

b)

null

c)

false

d)

true

6.

Which control structure is used to execute a block of code multiple times?

a)

Function

b)

Condition

c)

Loop

d)

Switch

7.

What is the purpose of the 'break' statement in Java?

a)

The 'break' statement is used to skip the current iteration of a loop.

b)

The 'break' statement is used to define a new method in Java.

c)

The 'break' statement is used to pause execution of a program.

d)

The 'break' statement is used to exit loops or switch statements.

8.

Which of the following is NOT a valid loop in Java?

a)

repeat-until

b)

foreach

c)

while

d)

do-while

9.

What is the output of the following code: System.out.println(10 > 5 ? 'A' : 'B');?

a)

C

b)

A

c)

B

d)

10

10.

What is the main principle of Object-Oriented Programming?

a)

Data encapsulation only

b)

Static typing only

c)

Encapsulation, inheritance, polymorphism, and abstraction.

d)

Single inheritance only

11.

Which of the following is a feature of Object-Oriented Programming?

a)

Encapsulation

b)

Inheritance

c)

Abstraction

d)

Polymorphism

12.

What keyword is used to inherit a class in Java?

a)

implements

b)

inherits

c)

derives

d)

extends

13.

What is the purpose of the 'this' keyword in Java?

a)

The 'this' keyword is used to refer to the current object instance in Java.

b)

The 'this' keyword is used to access global variables in Java.

c)

The 'this' keyword is used to create new classes in Java.

d)

The 'this' keyword is used to define static methods in Java.

14.

Which of the following is an example of method overloading?

a)

Overriding a method in a subclass with the same signature.

b)

Defining multiple methods with the same name but different parameter lists.

c)

Creating a single method with multiple return types.

d)

Defining methods with different names but the same parameters.

15.

What is encapsulation in Object-Oriented Programming?

a)

Encapsulation is the process of creating multiple instances of a class.

b)

Encapsulation refers to the inheritance of properties from a parent class.

c)

Encapsulation is the method of exposing all data and methods of an object to the outside world.

d)

Encapsulation is the bundling of data and methods that operate on that data, restricting access to some of the object's components.

16.

What is the output of the following code: int x = 10; if (x < 5) { System.out.println('Low'); } else { System.out.println('High'); }?

a)

Low

b)

Very High

c)

High

d)

Medium

17.

Which of the following statements is true about arrays in Java?

a)

Arrays in Java are fixed in size and can hold elements of the same type.

b)

Arrays in Java are always initialized with default values.

c)

Arrays in Java can hold elements of different types.

d)

Arrays in Java can grow dynamically in size.

18.

What is the correct syntax for a for loop in Java?

a)

for(initialization, condition, increment) { // loop body }

b)

for(initialization; increment; condition) { // loop body }

c)

for(initialization; condition; increment) { // loop body }

d)

for(condition; initialization; increment) { // loop body }

19.

What is polymorphism in Object-Oriented Programming?

a)

Polymorphism is the ability to create multiple instances of a class.

b)

Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface.

c)

Polymorphism is the ability of a class to inherit properties from multiple classes.

d)

Polymorphism refers to the process of combining multiple classes into one.

20.

Which of the following is a valid way to create an object in Java?

a)

MyClass obj = create MyClass();

b)

MyClass obj = new MyClass();

c)

MyClass obj = MyClass();

d)

MyClass obj : new MyClass();

21.

What is the purpose of the 'super' keyword in Java?

a)

The 'super' keyword is used to call a method from the parent class.

b)

The 'super' keyword is used to access private members of a class.

c)

The 'super' keyword is used to create a new instance of a class.

d)

The 'super' keyword is used to define a static variable.

22.

Which of the following is a characteristic of an abstract class in Java?

a)

An abstract class cannot be instantiated.

b)

All methods in an abstract class must be abstract.

c)

An abstract class must have at least one abstract method.

d)

An abstract class can have constructors.

23.

What is the output of the following code: String str = "Hello"; System.out.println(str.length());?

a)

5

b)

4

c)

6

d)

5.0

24.

Which of the following is a characteristic of a constructor in Java?

a)

A constructor can have a return type.

b)

A constructor is called when an object is created.

c)

A constructor must be private.

d)

A constructor can be inherited.

25.

What is the output of the following code: System.out.println("Hello".toUpperCase());?

a)

Hello

b)

HELLO!

c)

hello

d)

HELLO

26.

Which of the following keywords is used to define an interface in Java?

a)

interface

b)

implements

c)

class

d)

abstract

27.

What is the output of the following code: int[] arr = {1, 2, 3}; System.out.println(arr[1]);?

a)

3

b)

Array Index Out of Bounds

c)

2

d)

1

28.

Which of the following is a valid way to declare a method in Java?

a)

myMethod void() {}

b)

void: myMethod() {}

c)

myMethod() void {}

d)

void myMethod() {}

29.

What is the purpose of the 'final' keyword in Java?

a)

The 'final' keyword is used to prevent method overriding.

b)

The 'final' keyword is used to declare constants.

c)

All of the above.

d)

The 'final' keyword is used to prevent inheritance.

30.

What is the purpose of the 'static' keyword in Java?

a)

To indicate that a method is abstract.

b)

To create a constant variable.

c)

To declare a variable that can be modified.

d)

To define a method that can be called without creating an instance of the class.

31.

Which of the following is true about method overriding in Java?

a)

It is not allowed in Java.

b)

It can only be done with static methods.

c)

It requires the method in the subclass to have a different name.

d)

It allows a subclass to provide a specific implementation of a method already defined in its superclass.

32.

What is the output of the following code: String str = "Java"; System.out.println(str.charAt(2));?

a)

J

b)

v

c)

a

d)

Index Out of Bounds

33.

What is the output of the following code: String str = "Java"; System.out.println(str.charAt(2));?

a)

v

b)

J

c)

Java

d)

a

34.

Which of the following statements is true about interfaces in Java?

a)

Interfaces can have constructors.

b)

Interfaces can extend multiple other interfaces.

c)

Interfaces can have method implementations.

d)

Interfaces can be instantiated directly.

35.

What is the purpose of the 'this' keyword in Java?

a)

To call a static method.

b)

To refer to the current object.

c)

To refer to the parent class.

d)

To create a new instance of a class.

36.

What is the output of the following code: String str = "Hello World"; System.out.println(str.substring(6, 11));?

a)

World!

b)

o W

c)

World

d)

Hello

37.

Which of the following is a valid way to create an array in Java?

a)

int arr = new int[5];

b)

int arr = new int[];

c)

int arr[] = int[5];

d)

int[] arr = new int[5];

38.

What is the output of the following code: System.out.println(10 == 10 ? "Equal" : "Not Equal");?

a)

True

b)

10

c)

Not Equal

d)

Equal

39.

What is the purpose of the 'super' keyword in Java?

a)

To call a method from the parent class.

b)

To create a new instance of a class.

c)

To define a constant variable.

d)

To refer to the current class.

40.

Which of the following is a feature of Java's exception handling?

a)

Exceptions must be caught or declared to be thrown.

b)

All exceptions are checked exceptions.

c)

Exceptions can only be handled in the main method.

d)

Java does not support exception handling.

41.

What is the output of the following code: String str = "Hello"; System.out.println(str.length());?

a)

5

b)

4

c)

6

d)

Null

42.

What is the output of the following code: System.out.println(3 * 4 + 2);?

a)

8

b)

10

c)

12

d)

14

43.

Which of the following is a valid way to declare a method in Java?

a)

void myMethod() {}

b)

myMethod() void {}

c)

void: myMethod() {}

d)

myMethod void() {}

44.

What is the output of the following code: String str = "Hello"; System.out.println(str.length());?

a)

5.0

b)

6

c)

4

d)

5

45.

What is the output of the following code: int x = 5; System.out.println(x++);?

a)

6

b)

Compilation Error

c)

Runtime Error

d)

5

46.

Which of the following is a valid way to implement an interface in Java?

a)

class MyClass : MyInterface {}

b)

class MyClass implements MyInterface()

c)

class MyClass extends MyInterface {}

d)

class MyClass implements MyInterface {}

47.

What is the purpose of the 'this' keyword in Java?

a)

To refer to the parent class.

b)

To call a static method.

c)

To create a new instance of a class.

d)

To refer to the current object instance.

48.

What is the output of the following code: int a = 10; int b = 20; System.out.println(a + b);?

a)

20

b)

10

c)

30

d)

1020

49.

Which of the following is a valid way to declare a constructor in Java?

a)

void MyClass() {}

b)

MyClass() {}

c)

MyClass() : {}

d)

MyClass {}

50.

What is the purpose of the 'final' keyword in Java?

a)

To declare a constant variable.

b)

To prevent method overriding.

c)

To prevent inheritance of a class.

d)

All of the above.