NEW
Font size
WorksheetsJava Fundamentals: OOP, Memory, and Data Types (Q1-11)
Total questions: 75
Worksheet time: 38mins
Which of the following is not one of the four main principles of Object-Oriented Programming (OOP)?
Encapsulation
Abstraction
Polymorphism
Inheritance
In Java, where are objects like instances of classes and arrays typically allocated in memory?
CPU cache memory
Stack memory
Method area
Heap memory
What is the main purpose of the stack memory in Java?
To manage memory for local variables and method call information
To store objects and arrays
To store program instructions
To allocate memory for dynamic data structures
What does JVM stand for?
Java Virtual Machine
Java Virtual Method
Java Variable Manager
Java Variable Machine
Which component of Java is responsible for executing Java bytecode?
JIT
JVM
JDK
JRE
What is the primary function of the Java Runtime Environment (JRE)?
Compiling Java source code
Executing Java applications
Debugging Java applications
Developing Java applications
In Java, which component is responsible for optimizing code execution for improved performance?
JVM
JRE
JIT compiler
Class Loader
Which part of the Java Virtual Machine is responsible for reclaiming memory occupied by objects no longer in use?
JVM
JRE
JIT compiler
Garbage Collector
Which component of Java is responsible for providing the development tools, libraries, and runtime environment for building and running Java applications?
JRE
JIT compiler
JVM
JDK
Which primitive data type in Java is used to store a single character?
short
char
boolean
byte
Which of the following is a reference type in Java?
double
long
Integer
char
Reference types in Java are typically used to store:
Booleans
Primitive values
Single characters
Objects and class instances
Which of the following is a reference type used to store a sequence of characters in Java?
double
int
float
String
What is the primary difference between float and double data types in Java?
double has a smaller range
float has higher precision
double has higher precision
float has a larger range
Which data type is suitable for storing a person's age in Java?
int
double
short
long
float
Which data type should be used to store a large population number, such as the world's population?
double
float
short
long
int
In terms of memory usage, which data type consumes the most memory in Java?
int
short
float
long
double
What is the recommended convention for naming Java classes?
camelCase
PascalCase
UPPERCASE
snake_case
Which of the following is a good example of a Java constant (final variable) name?
ConstantValue
constant-value
PI_VALUE
CONSTANTVALUE
In Java, what is the convention for naming methods or functions?
snake_case
camelCase
PascalCase
UPPERCASE
What is the naming convention for Java interface names?
PascalCase
snake_case
camelCase
UPPERCASE
Int i = 3, j = 3; i++; j--; Identify the operators used for i and j.
prefix increment and prefix decrement
postfix increment and postfix decrement
post increment and pre increment
pre decrement and pre increment
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); } }
129
-127
0
127
Select the valid statement.
char[] ch = new char(5)
char[] ch = new char[]
char[] ch = new char[5]
char[] ch = new char()
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.
I, II and III
Only III
0
I and II
Identify what can directly access and change the value of the variable res. package com.mypackage; public class Solution{ private int res = 100; }
Any class
Only Solution class
Any class that extends Solution
None
In which of the following is toString() method defined?
java.lang.String
java.lang.util
java.lang.Object
None
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)); } }
one
onetwo
twoone
one two
What does the following do to given string str1? String str1 = "Interviewbit".replace('e','s');
Replaces all occurrence of "e" to "s"
Replaces single occurrence of "s" to "e"
Replaces single occurrence of "e" to "s"
None
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";
2
0
4
3
Find the output of the code: if(1 + 1 + 1 + 1 + 1 == 5){ System.out.print("TRUE"); } else{ System.out.print("FALSE"); }
None
True
False
Compile error
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); } }
50
22
0
10
Identify the modifier which cannot be used for a constructor.
Private
Protected
Public
Static
Identify the correct way of declaring a constructor for class Solution.
Solution(void){}
A and B
Public Solution(){}
Solution(){}
Which of the following is not one of the four main principles of Object-Oriented Programming (OOP)?
Inheritance
Encapsulation
Polymorphism
Abstraction
In Java, what is an abstract method?
A method with no body
A method that is not accessible
A method with a private access modifier
A method that cannot be overridden
What is the purpose of an abstract class in Java?
To create objects directly
To define methods without implementation
To hide data
To prevent inheritance
Which of the following is not an access modifier in Java?
default
private
internal
public
What is the purpose of the this keyword in Java?
It is a data type
It is a reserved keyword
It is used for loop iterations
It refers to the current class object
In Java, can you override a private method in a subclass?
No
Yes
What is the primary purpose of the super keyword in Java?
To declare constants
To create objects
To access static methods
To call a superclass constructor
Which statement is true about a constructor in Java?
It has the same name as the class
It returns a value
It cannot have parameters
It can be inherited
What is the purpose of the break statement in Java?
To skip the current iteration of a loop
To terminate a program
To exit a loop or switch statement
To continue the loop from the beginning
What will be the output of the following Java code?
import java.util.*;
public class genericstack
0
36
Compilation Error
Runtime Error
What will be the output of the following Java code?
import java.util.*;
public class genericstack
Error
Hello
36
Hello 36
Encapsulation is related to:
Inheritance
Data hiding
Code optimization
Polymorphism
Encapsulation promotes the use of which access modifier for class attributes that should not be directly accessed from outside the class?
protected
package-private (default)
private
public
What is an Unbounded Wildcard?
List> wildcardList = new ArrayList
public static void addDogs(List super Animal> list) { list.add(new Dog("tom")) }
public static void makeLotsOfNoise(List extends Animal> animals) { animals.forEach(Animal::makeNoise); }
none
What is a Lower Bounded Wildcard?
none
public static void addDogs(List super Animal> list) { list.add(new Dog("tom")) }
List> wildcardList = new ArrayList
public static void makeLotsOfNoise(List extends Animal> animals) { animals.forEach(Animal::makeNoise); }
What is an Upper Bounded Wildcard?
none
public static void makeLotsOfNoise(List extends Animal> animals) { animals.forEach(Animal::makeNoise); }
public static void addDogs(List super Animal> list) { list.add(new Dog("tom")) }
List> wildcardList = new ArrayList
In method overriding, which keyword is used to explicitly indicate that a method is intended to override a superclass method?
super
extends
override
new
Which of the following statements about method overloading is true?
Overloaded methods must belong to different classes.
Overloaded methods can have the same name but different parameter types.
Overloaded methods can have the same number of parameters.
Overloaded methods must have the same return type.
Which of the following keywords is used to call the superclass version of an overridden method from the subclass?
extends
this
super
override
In Java, polymorphism can be best described as:
The ability of an object to take on many forms.
The ability to perform type casting.
The process of creating new classes.
The implementation of multiple constructors.
Which of these keywords can be used to prevent Method overriding?
static
constant
protected
final
Which of these is a super class of wrappers Double and Float?
Long
Digits
Float
Number
What is the extension of java code files?
.java
.class
.js
.txt
Which exception is thrown when Java is out of memory?
MemoryOutOfBoundsException
MemoryFullException
OutOfMemoryError
MemoryError
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); } }
QuizziuQ
ziuQuiz
QuiZ
ziuQ
What is the use of try & catch?
All of the mentioned
It prevents automatic terminating of the program in cases when an exception occurs.
It allows to fix errors.
It allows us to manually handle the exception.
Which of the following is a mutable class in Java?
java.lang.StringBuilder
java.lang.String
java.lang.Byte
java.lang.Short
Which of these statements is incorrect?
try block can be followed by finally block instead of catch block
try block need not to be followed by catch block
try can be followed by both catch and finally block
try need not to be followed by anything
Which interface does ArrayList implement?
List
Queue
Map
Set
What is the primary difference between HashMap and TreeMap?
HashMap allows null keys, TreeMap does not
They have the same functionality
TreeMap is unordered, HashMap is sorted
HashMap is faster for unsorted data, TreeMap maintains a natural order
Which method is used to create an unmodifiable collection in Java?
Stream.unmodifiable()
new ImmutableList()
List.of()
Collections.unmodifiableList()
Which class maintains elements in sorted order?
TreeSet
ArrayList
HashSet
LinkedHashSet
Which of the following not allows duplicate elements?
Queue
Set
ArrayList
Map
Which interface is implemented by PriorityQueue?
Map
Set
Queue
Deque
What is the primary advantage of LinkedList over ArrayList?
Faster random access
Faster insertions and deletions
Immutable elements
Better memory usage
Which method is used to add an element at a specific index in a List?
add()
put()
insert()
append()
What is the output of the given Java program that sorts a HashMap by values in descending order and prints entries?
Banana:5, Orange:2, Apple:3
Apple:3, Banana:5, Orange:2
Banana:5, Apple:3, Orange:2
Orange:2, Apple:3, Banana:5
Given the code: public class Main { public static void main(String[] args) { int number; System.out.println(number); } }, what will happen when compiled?
Prints 0 to console
Compilation error occurs
Prints an undefined value
Throws NullPointerException
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?
World
HelloWorld
Compilation Error
Hello
For the code snippet: abstract class MyFirstClass { abstract num (int a, int b) { } }, what is the outcome?
Extra parentheses
No error
Method is not defined properly
Constructor is not defined properly
In Java, which List method adds an element at a specific index with shifting of subsequent elements?
set(int, E) replaces value
add(int, E) inserts element
addAll(Collection) merges list
remove(int) deletes element
