wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Fundamentals: OOP, Memory, and Data Types (Q1-11)

Total questions: 75

Worksheet time: 38mins

Name
Class
Date
1.

Which of the following is not one of the four main principles of Object-Oriented Programming (OOP)?

a)

Encapsulation

b)

Abstraction

c)

Polymorphism

d)

Inheritance

2.

In Java, where are objects like instances of classes and arrays typically allocated in memory?

a)

CPU cache memory

b)

Stack memory

c)

Method area

d)

Heap memory

3.

What is the main purpose of the stack memory in Java?

a)

To manage memory for local variables and method call information

b)

To store objects and arrays

c)

To store program instructions

d)

To allocate memory for dynamic data structures

4.

What does JVM stand for?

a)

Java Virtual Machine

b)

Java Virtual Method

c)

Java Variable Manager

d)

Java Variable Machine

5.

Which component of Java is responsible for executing Java bytecode?

a)

JIT

b)

JVM

c)

JDK

d)

JRE

6.

What is the primary function of the Java Runtime Environment (JRE)?

a)

Compiling Java source code

b)

Executing Java applications

c)

Debugging Java applications

d)

Developing Java applications

7.

In Java, which component is responsible for optimizing code execution for improved performance?

a)

JVM

b)

JRE

c)

JIT compiler

d)

Class Loader

8.

Which part of the Java Virtual Machine is responsible for reclaiming memory occupied by objects no longer in use?

a)

JVM

b)

JRE

c)

JIT compiler

d)

Garbage Collector

9.

Which component of Java is responsible for providing the development tools, libraries, and runtime environment for building and running Java applications?

a)

JRE

b)

JIT compiler

c)

JVM

d)

JDK

10.

Which primitive data type in Java is used to store a single character?

a)

short

b)

char

c)

boolean

d)

byte

11.

Which of the following is a reference type in Java?

a)

double

b)

long

c)

Integer

d)

char

12.

Reference types in Java are typically used to store:

a)

Booleans

b)

Primitive values

c)

Single characters

d)

Objects and class instances

13.

Which of the following is a reference type used to store a sequence of characters in Java?

a)

double

b)

int

c)

float

d)

String

14.

What is the primary difference between float and double data types in Java?

a)

double has a smaller range

b)

float has higher precision

c)

double has higher precision

d)

float has a larger range

15.

Which data type is suitable for storing a person's age in Java?

a)

int

b)

double

c)

short

d)

long

e)

float

16.

Which data type should be used to store a large population number, such as the world's population?

a)

double

b)

float

c)

short

d)

long

e)

int

17.

In terms of memory usage, which data type consumes the most memory in Java?

a)

int

b)

short

c)

float

d)

long

e)

double

18.

What is the recommended convention for naming Java classes?

a)

camelCase

b)

PascalCase

c)

UPPERCASE

d)

snake_case

19.

Which of the following is a good example of a Java constant (final variable) name?

a)

ConstantValue

b)

constant-value

c)

PI_VALUE

d)

CONSTANTVALUE

20.

In Java, what is the convention for naming methods or functions?

a)

snake_case

b)

camelCase

c)

PascalCase

d)

UPPERCASE

21.

What is the naming convention for Java interface names?

a)

PascalCase

b)

snake_case

c)

camelCase

d)

UPPERCASE

22.

Int i = 3, j = 3; i++; j--; Identify the operators used for i and j.

a)

prefix increment and prefix decrement

b)

postfix increment and postfix decrement

c)

post increment and pre increment

d)

pre decrement and pre increment

23.

Find the output of the following program. public class Solution{ public static void main(String[] args){ byte x = 127; x++; x++; System.out.print(x); } }

a)

129

b)

-127

c)

0

d)

127

24.

Select the valid statement.

a)

char[] ch = new char(5)

b)

char[] ch = new char[]

c)

char[] ch = new char[5]

d)

char[] ch = new char()

25.

Identify the correct restriction on static methods. 1. They must access only static data 2. They can only call other static methods. 3. They cannot refer to this or super.

a)

I, II and III

b)

Only III

c)

0

d)

I and II

26.

Identify what can directly access and change the value of the variable res. package com.mypackage; public class Solution{ private int res = 100; }

a)

Any class

b)

Only Solution class

c)

Any class that extends Solution

d)

None

27.

In which of the following is toString() method defined?

a)

java.lang.String

b)

java.lang.util

c)

java.lang.Object

d)

None

28.

Identify the output of the following program. public class Test{ public static void main(String args[]){ String str1 = "one"; String str2 = "two"; System.out.println(str1.concat(str2)); } }

a)

one

b)

onetwo

c)

twoone

d)

one two

29.

What does the following do to given string str1? String str1 = "Interviewbit".replace('e','s');

a)

Replaces all occurrence of "e" to "s"

b)

Replaces single occurrence of "s" to "e"

c)

Replaces single occurrence of "e" to "s"

d)

None

30.

How many objects will be created in the following? String a = new String("Interviewbit"); String b = new String("Interviewbit"); String c = "Interviewbit"; String d = "Interviewbit";

a)

2

b)

0

c)

4

d)

3

31.

Find the output of the code: if(1 + 1 + 1 + 1 + 1 == 5){ System.out.print("TRUE"); } else{ System.out.print("FALSE"); }

a)

None

b)

True

c)

False

d)

Compile error

32.

Find the output of the code: public class Solution{ public static void main(String... argos){ int x = 5; x *= (3 + 7); System.out.println(x); } }

a)

50

b)

22

c)

0

d)

10

33.

Identify the modifier which cannot be used for a constructor.

a)

Private

b)

Protected

c)

Public

d)

Static

34.

Identify the correct way of declaring a constructor for class Solution.

a)

Solution(void){}

b)

A and B

c)

Public Solution(){}

d)

Solution(){}

35.

Which of the following is not one of the four main principles of Object-Oriented Programming (OOP)?

a)

Inheritance

b)

Encapsulation

c)

Polymorphism

d)

Abstraction

36.

In Java, what is an abstract method?

a)

A method with no body

b)

A method that is not accessible

c)

A method with a private access modifier

d)

A method that cannot be overridden

37.

What is the purpose of an abstract class in Java?

a)

To create objects directly

b)

To define methods without implementation

c)

To hide data

d)

To prevent inheritance

38.

Which of the following is not an access modifier in Java?

a)

default

b)

private

c)

internal

d)

public

39.

What is the purpose of the this keyword in Java?

a)

It is a data type

b)

It is a reserved keyword

c)

It is used for loop iterations

d)

It refers to the current class object

40.

In Java, can you override a private method in a subclass?

a)

No

b)

Yes

41.

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

a)

To declare constants

b)

To create objects

c)

To access static methods

d)

To call a superclass constructor

42.

Which statement is true about a constructor in Java?

a)

It has the same name as the class

b)

It returns a value

c)

It cannot have parameters

d)

It can be inherited

43.

What is the purpose of the break statement in Java?

a)

To skip the current iteration of a loop

b)

To terminate a program

c)

To exit a loop or switch statement

d)

To continue the loop from the beginning

44.

What will be the output of the following Java code? import java.util.*; public class genericstack { Stack stk = new Stack (); public void push(E obj) { stk.push(obj); } public E pop() { E obj = stk.pop(); return obj; } } class Output { public static void main(String args[]) { genericstack gs = new genericstack(); gs.push(36); System.out.println(gs.pop()); } }

a)

0

b)

36

c)

Compilation Error

d)

Runtime Error

45.

What will be the output of the following Java code? import java.util.*; public class genericstack { Stack stk = new Stack (); public void push(E obj) { stk.push(obj); } public E pop() { E obj = stk.pop(); return obj; } } class Output { public static void main(String args[]) { genericstack gs = new genericstack(); gs.push("Hello"); System.out.print(gs.pop() + " "); genericstack gs2 = new genericstack(); gs2.push(36); System.out.println(gs2.pop()); } }

a)

Error

b)

Hello

c)

36

d)

Hello 36

46.

Encapsulation is related to:

a)

Inheritance

b)

Data hiding

c)

Code optimization

d)

Polymorphism

47.

Encapsulation promotes the use of which access modifier for class attributes that should not be directly accessed from outside the class?

a)

protected

b)

package-private (default)

c)

private

d)

public

48.

What is an Unbounded Wildcard?

a)

List wildcardList = new ArrayList();

b)

public static void addDogs(List list) { list.add(new Dog("tom")) }

c)

public static void makeLotsOfNoise(List animals) { animals.forEach(Animal::makeNoise); }

d)

none

49.

What is a Lower Bounded Wildcard?

a)

none

b)

public static void addDogs(List list) { list.add(new Dog("tom")) }

c)

List wildcardList = new ArrayList();

d)

public static void makeLotsOfNoise(List animals) { animals.forEach(Animal::makeNoise); }

50.

What is an Upper Bounded Wildcard?

a)

none

b)

public static void makeLotsOfNoise(List animals) { animals.forEach(Animal::makeNoise); }

c)

public static void addDogs(List list) { list.add(new Dog("tom")) }

d)

List wildcardList = new ArrayList();

51.

In method overriding, which keyword is used to explicitly indicate that a method is intended to override a superclass method?

a)

super

b)

extends

c)

override

d)

new

52.

Which of the following statements about method overloading is true?

a)

Overloaded methods must belong to different classes.

b)

Overloaded methods can have the same name but different parameter types.

c)

Overloaded methods can have the same number of parameters.

d)

Overloaded methods must have the same return type.

53.

Which of the following keywords is used to call the superclass version of an overridden method from the subclass?

a)

extends

b)

this

c)

super

d)

override

54.

In Java, polymorphism can be best described as:

a)

The ability of an object to take on many forms.

b)

The ability to perform type casting.

c)

The process of creating new classes.

d)

The implementation of multiple constructors.

55.

Which of these keywords can be used to prevent Method overriding?

a)

static

b)

constant

c)

protected

d)

final

56.

Which of these is a super class of wrappers Double and Float?

a)

Long

b)

Digits

c)

Float

d)

Number

57.

What is the extension of java code files?

a)

.java

b)

.class

c)

.js

d)

.txt

58.

Which exception is thrown when Java is out of memory?

a)

MemoryOutOfBoundsException

b)

MemoryFullException

c)

OutOfMemoryError

d)

MemoryError

59.

What will be the output of the following Java program? class output { public static void main(String args[]) { StringBuffer s1 = new StringBuffer("Quiz"); StringBuffer s2 = s1.reverse(); System.out.println(s2); } }

a)

QuizziuQ

b)

ziuQuiz

c)

QuiZ

d)

ziuQ

60.

What is the use of try & catch?

a)

All of the mentioned

b)

It prevents automatic terminating of the program in cases when an exception occurs.

c)

It allows to fix errors.

d)

It allows us to manually handle the exception.

61.

Which of the following is a mutable class in Java?

a)

java.lang.StringBuilder

b)

java.lang.String

c)

java.lang.Byte

d)

java.lang.Short

62.

Which of these statements is incorrect?

a)

try block can be followed by finally block instead of catch block

b)

try block need not to be followed by catch block

c)

try can be followed by both catch and finally block

d)

try need not to be followed by anything

63.

Which interface does ArrayList implement?

a)

List

b)

Queue

c)

Map

d)

Set

64.

What is the primary difference between HashMap and TreeMap?

a)

HashMap allows null keys, TreeMap does not

b)

They have the same functionality

c)

TreeMap is unordered, HashMap is sorted

d)

HashMap is faster for unsorted data, TreeMap maintains a natural order

65.

Which method is used to create an unmodifiable collection in Java?

a)

Stream.unmodifiable()

b)

new ImmutableList()

c)

List.of()

d)

Collections.unmodifiableList()

66.

Which class maintains elements in sorted order?

a)

TreeSet

b)

ArrayList

c)

HashSet

d)

LinkedHashSet

67.

Which of the following not allows duplicate elements?

a)

Queue

b)

Set

c)

ArrayList

d)

Map

68.

Which interface is implemented by PriorityQueue?

a)

Map

b)

Set

c)

Queue

d)

Deque

69.

What is the primary advantage of LinkedList over ArrayList?

a)

Faster random access

b)

Faster insertions and deletions

c)

Immutable elements

d)

Better memory usage

70.

Which method is used to add an element at a specific index in a List?

a)

add()

b)

put()

c)

insert()

d)

append()

71.

What is the output of the given Java program that sorts a HashMap by values in descending order and prints entries?

a)

Banana:5, Orange:2, Apple:3

b)

Apple:3, Banana:5, Orange:2

c)

Banana:5, Apple:3, Orange:2

d)

Orange:2, Apple:3, Banana:5

72.

Given the code: public class Main { public static void main(String[] args) { int number; System.out.println(number); } }, what will happen when compiled?

a)

Prints 0 to console

b)

Compilation error occurs

c)

Prints an undefined value

d)

Throws NullPointerException

73.

In the program: class Output { public static void main(String args[]) { try { int a = 0; int b = 5; int c = b / a; System.out.print("Hello"); } catch(Exception e) { System.out.print("World"); } } }, what will be printed?

a)

World

b)

HelloWorld

c)

Compilation Error

d)

Hello

74.

For the code snippet: abstract class MyFirstClass { abstract num (int a, int b) { } }, what is the outcome?

a)

Extra parentheses

b)

No error

c)

Method is not defined properly

d)

Constructor is not defined properly

75.

In Java, which List method adds an element at a specific index with shifting of subsequent elements?

a)

set(int, E) replaces value

b)

add(int, E) inserts element

c)

addAll(Collection) merges list

d)

remove(int) deletes element