Font size
WorksheetsJava Programming Quiz
Total questions: 55
Worksheet time: 29mins
Which of these can an abstract class contain?
Only concrete methods
Static,private
Constructors
Final
all
What is the purpose of the TreeSet class in Java?
It allows duplicate elements?
It does not allow duplicate elements and maintains a sorted order.
It allows duplicate elements and maintains insertion order
It does not allow duplicate elements and maintains insertion order
Can you declare variables inside an interface?
No, interfaces can only have methods
Yes, but they must be private
Yes, and they are implicitly public and static
Only if they are final and static
Suppose the class Cat extends the class Animal. Which of the following are legal?
Animal x = new Animal();
Animal x = new Animal();
x = new Cat();
x = new Animal();
x = y
y = x
Which of the following collections is thread-safe?
ArrayList
Vector
Мар
LinkedList
TreeSet
What does JDK stand for?
Collection of Java programming language's syntax and semantics
The software for compiling and running Java programs
The Java application program interface
The software for developing Java programs
The program for running Java programs
Which requirements are true to create immutable class?
Class must have only immutable fields
No accessor methods can return a reference to a data field that is mutable
All data fields must be private
There should be User methods for data fields
All answers are correct
Select objects that are stored in stack memory.
heap memory
arraylist
array
int, char, boolean, floa
Which type of method of superclass we cannot override in subclasses?
final
private
Static
Abstract
All
What is the output of the following Java program?
public class Vehicle {
public void move() {
System.out.println("The vehicle moves");
}
}
public class Car extends Vehicle {
public void move() {
System.out.println("The car moves");
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.move();
}
}
The vehicle moves
The car moves
Vehicle vehicle = new Car()
What is encapsulation?
Sharing field value by all objects of this class
The definition of behavior of the object
Hiding internal data from the outside world
A collection of methods with no implementation
Having several method with the same name and different arguments
Which of the following classes can't be subclassed?
public class Test (.)
public static class Test (,)
public interface Test (.)
public final class Test (.)
public abstract class Test (.)
Which statement is true regarding polymorphism?
An overridden method can be less restricDve than the superclass method.
An overridden method can be more restricDve than the superclass method
All answers
Overtoaded methods cannot change the return typ
Constructors can be overridden
Which of the following best describes the volatile keyword in Java?
It ensures changes to a variable are visible to all threads.
It marks a variable for garbage collection.
It guarantees atomicity for read and write operations.
It allows a variable to be accessed by only one thread at a time.
What will be the output?
public static void main(String[] args) {
List intList = new ArrayList <> ();
intList.add(10);
intList.add(20);
System.out.println("The list is: " + intList);
}
The list is: [10, 20]
The list is: [20, 20]
The list is: [20, 10]
complition error
List methods to copy one array to another, so that they will point to the separate memory addresses
Use equating (=)
Use the static arraycopy method in the System class.
All answers
Use a loop to copy individual elements one by one.
Use the clone method to copy arrays;
class Animal {
String name = "animal";
String message() {
return "from animal";
}
}
class Child extends Animal {
String name = "child";
@Override
String message() {
return "from child";
}
}
public class Main {
public static void main(String[] args) {
Animal p = new Child();
System.out.println(p.name); // Accessing the 'name' field
}
}
animal
child
child from child
animal from aniimal
Choose collection, which ensures that elements are processed in first-in-first-out (FIFO) order?
PriorityQueue
LinkedList
TreeSet
HashSet
Which keyword is used to prevent a class from being subclassed in Java?
Package
Final
abstract
static
private
What is the primary purpose of using the ArrayList class in Java over regular arrays?
ArrayList can store only unique elements.
ArrayList consumes less memory than arrays.
ArrayList allows you to store primitive types directly.
ArrayList automatically adjusts its size during runtime.
Which of the following are checked exceptions?
IOException
NullPointerException
IndexOutOfBoundsException
Suppose, we have class Person, with field namo and constructor with arguments for name, what will be the output of the following code: public static void main(Stringl args) ( Person t = new Person("A"): changePerson(t); System.out.printin(t.nam; private static void changePerson(Person t) ( t.name = "B";
The program will throw an exception
Null
B
A
Which requirements are true to create immutable class?
All answers are correct
final
private
static
Choose statements where the literal is presented
Object o = new Object:
String s;
int count = 0:
boolean b = true;
void printo;
True or false?
a. An interface is compiled into a separate bytecode file.
b. An interface can have static methods.
c. An interface can extend one or more interfaces.
d. An interface can extend an abstract class.
e. An interface can have default methods.
a. True
b. False
c. True
d. True
e. True in Java 8
a. True
b. True
c. True
d. False
e. True in Java 8
a. False
b. False
c. True
d. True
e. True in Java 8
a. True
b. True
c. False
d. False
e. Falsein Java 8
What is a thread in Java?
A lightweight process that executes a sequence of instructions.
A data structure to store multiple values.
A Java keyword to define a loop construct.
A memory allocation unit in Java
Which class provides methods to create a client-side socket in Java?
Socket
ServerSocket
ClientSocket
Servlet
What will be the output?
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
start();
System.out.println("in constructor " + getName());
}
@Override
public void start() {
System.out.println("in start " + getName());
}
public void run() {
System.out.println("in run " + getName());
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread th = new MyThread("oops");
}
}
in start oops
in constructor oops
in run oops
in constructor oops in start oops in run oops
in start oops
in constructor oops
Show the output of the following code:
int x = 6;
int y = a++;
System.out.println(x);
System.out.println(y);
x = 6;
y = ++a;
System.out.println(x);
System.out.println(y);
7
6
7
7
6
7
6
7
6
7
5
7
6
7
7
7
Convert for loop to while loopint
sum = 0;
for (int i = 0; i < 4; i++) {
if (i % 3 == 0) continue;
sum += i;
}
int sum = 0;
int i = 0;
while (i < 4) {
if (i % 3 == 0) continue;
sum += i;
i++;
}
int sum = 0;
int i = 0;
while (i < 4) {
if (i % 3 == 0) {
i++;
continue;
}
sum += i;
i++;
}
int sum = 0;
int i = 0;
while (i < 4) {
if (i % 2 == 0) continue;
sum += i;
i++;
}
What is polymorphism?
The ability of objects of different classes to respond to the same method in a way that is specific to their individual class
Restricting access to the declaring classes
Promoting code reusability and establishing an 'is-a' relationship between classes
Hiding the internal implementation details and restricts access to data through defined interfaces
A special method used to initialize objects
Which of the following is a correct interface? Explain each of them why it is correct or not
(a)
interface A {
void print() { }
}
(b)
abstract interface A {
abstract void print() { }
}
(c)
abstract interface A {
print();
}
(d)
interface A {
void print();
}
(e)
interface A {
default void print() {
}
}
(f)
interface A {
static int get() {
return 0;
}
}
a,b,c
a,b,c
a,e,b
c,f,d
a,c,e
What is the purpose of the join method in Java threads?
To wait for a thread to finish its execution.
To notify a thread to stop execution.
To schedule the execution of a thread.
To interrupt the execution of a thread.
Can a class extend multiple classes? (Y/N)
Can a class implement multiple interfaces? (Y/N)
Can an interface extend multiple interfaces? (Y/N)
N
Y
Y
N
Y
N
Y
Y
Y
Y
Y
N
True or falso or true in java 8
a. An interface is compiled into a separate bytecode file.
b. An interface can have static methods.
c. An interface can extend one or more interfaces.
d. An interface can extend an abstract class.
e. An interface can have default methods.
a. True
b. True in Java 8
c. True
d. False
True in Java 8
хз
String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
String s3 = "Welcome to Java";
String s4 = new String("Welcome to Java");
s1 == s3 -> will true
s3 == s4-> will true:
s1 == s2-> will true:
s1 == s3 -> will flase
s2 == s4-> will true:
Which of the following best describes the volatile keyword in Java?
It ensures changes to a variable are visible to all threads.
It guarantees atomicity for read and write operations.
It marks a variable for garbage collection.
It allows a variable to be accessed by only one thread at a time.
public static void main(String[] args) {
int i = 1;
do {
int num = 1;
for (int j = 1; j <= i; j++) {
System.out.print(num + "A");
num += 2;
}
System.out.println();
i++;
} while (i <= 3);
}
1A
1A3A
1A3A5A
1A
1A2A
1A2A3A
1A
1A3
1A2A5A
1A
1A2A
1A3A5A
Which statement is true regarding polymorphism?
Polymorphism allows objects of different classes to be treated as objects of a common superclass.
Polymorphism allows a method to have multiple implementations within the same class.
Polymorphism is achieved only through method overriding.
Polymorphism allows an object to change its type during runtime.
In polymorphism, a reference variable of the superclass can refer to the object of which classes?
Only the superclass
The superclass or any of its subclasses
Only the subclass
Any class
In which collecDon you cannot have duplicate elements?
Set
Array
Map
What is purpose of the join() method in java threads?
To notify a thread to stop execution
To interrupt the execution of a thread
To schedule the execution of a thread
To wait for a thread to finish its execution
Can we override a static method?
It depends on the access modifier of the static method.
No, static methods cannot be overridden because they belong to the class, not the object.
It depends on whether the static method is declared as final or not.
Yes, we can override a static method just like we do with instance methods.
It depends on the modifier (static or abstract) of class.
Which of these can an abstract class contain?
Both abstract and concrete methods
Only concrete methods
Only abstract methods
None of the above
Which collection provides a guaranteed constant time for basic operations like add, remove, and contains?
ArrayList
HashSet
TreeSet
LinkedList
Show the output of the following code:
float f = 12.5F;
int i = (int)f;
System.out.println("f is " + f);
System.out.println("i is " + i);
program will throw type cast exception
12.5
12
12.5f
12.5
12.5f
12.5f
Suppose you need to write a program that stores nonduplicate elements in increasing order of the element values, what data structure should you use?
Arraylist
Linkedhashset
Treeset
hashset
array
Фото какое то
Static method in parent
Static method in parent
Про это крч
It will print "Static method in Child".
It will print "Static method in Parent".
Show the output of the following code:
ArrayList list = new Arraylist<>);
list-add ("New York");
ArrayList listl = list;
ArrayList list2 = (ArrayList) (list.clone());
list.add("Atlanta");
System.out. printin(list = listi);
System.out-printin(list == list2);
System.out-printin("list is " + list);
System.out println("listl is " + listl);
System. out printin("list2.get(0) is " + list2-get (0));
System.out-println("list2.size() is " + list2.size());
true
false
list is [New York, Atlanta]
list1 is [New York, Atlanta]
list2.get(0) is New York
list2.size() is 1
хз
хз
хз
хз
Which interface in Java Collections Framework does not extend the Collection interface?
Queue
List
Map
Deque
Set
Which interface must a Java class implement to create a servlet?
HttpServlet
ServletRequest
ServletConfig
ServerSocket
Servlet
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5}; // Use curly braces {} for array initialization
int sum = 0; // Correct the initialization of sum (no 03 in Java)
for (int i = 0; i < numbers.length; i++) { // Replace ‹ with <, and ( with {
if (numbers[i] % 2 == 0) {
continue; // Skip even numbers
}
sum += numbers[i]; // Add odd numbers to sum
}
System.out.println("Sum: " + sum);
}
}
Sum: 6
Sum: 10
Sum: 9
Sum: 15
Which command is used to compile HelloWorld.java file?
java HelloWorld.java
javac HelloWorld.java
run java HelloWorld.java
Given following code. What will be the output?
public static void main(String[] args) {
Set setTest = Collections.singleton("Test");
setTest.add("Test2");
System.out.println(setTest);
}
UnsupportedOperationException
хз
хз
хз
What is the JVM used for?
All answers
To develop java program
To interpret java bytecode
To compile java file to class file
To compile and run java program
