Java Programming Language
Basics of Java
Question 1: Which of the following is true about Java?
a) Java is a low-level language
b) Java is a high-level language
c) Java is a machine language
d) Java is an assembly language
Answer: b) Java is a high-level language
Question 2: What is the correct syntax to include the standard input/output library in Java?
a) import [Link].*;
b) #include <iostream>
c) import [Link].*;
d) import [Link].*;
Answer: a) import [Link].*;
Question 3: Which method is invoked automatically when an object is created?
a) start()
b) run()
c) init()
d) constructor()
Answer: d) constructor()
Data Types and Variables
Question 4: What is the size of the int data type in Java?
a) 4 bytes
b) 8 bytes
c) 2 bytes
d) Depends on the platform
Answer: a) 4 bytes
Question 5: Which keyword is used to declare a constant in Java?
a) final
b) const
1
c) static
d) var
Answer: a) final
Question 6: What is the default value of an int variable in Java?
a) 0
b) 0.0
c) null
d) undefined
Answer: a) 0
Operators
Question 7: What does the ++ operator do in Java?
a) Increment by 1
b) Decrement by 1
c) Multiply by 2
d) Divide by 2
Answer: a) Increment by 1
Question 8: Which operator is used to compare two values for equality in Java?
a) ==
b) =
c) ===
d) !=
Answer: a) ==
Question 9: What is the result of 5 + 3 * 2 in Java?
a) 16
b) 11
c) 10
d) 13
Answer: b) 11
Control Structures
Question 10: Which statement is used to terminate the current loop iteration in Java?
2
a) break
b) continue
c) end
d) exit
Answer: b) continue
Question 11: What is the correct syntax for an if statement in Java?
a) if (x > 5) { ... }
b) if x > 5 then { ... }
c) if (x > 5) then { ... }
d) if x > 5 { ... }
Answer: a) if (x > 5) { ... }
Question 12: How many times will the following for loop execute?
for (int i = 0; i < 5; ++i) { ... }
a) 4
b) 5
c) 6
d) Infinite
Answer: b) 5
Methods and Functions
Question 13: Which keyword is used to define a method in Java?
a) func
b) method
c) function
d) void
Answer: d) void
Question 14: What is the purpose of method overloading in Java?
a) To reduce the number of methods in a class
b) To improve program performance
c) To provide multiple methods with the same name but different parameters
d) To prevent method calls
Answer: c) To provide multiple methods with the same name but different parameters
Question 15: What is the return type of a method that does not return any value in Java?
3
a) void
b) null
c) empty
d) None
Answer: a) void
Arrays and Strings
Question 16: How do you access the third element of an array arr in Java?
a) arr(3)
b) arr[3]
c) arr{3}
d) [Link]()
Answer: b) arr[3]
Question 17: What is the correct way to declare and initialize a string variable in Java?
a) String s = "Hello";
b) char s = "Hello";
c) char[] s = "Hello";
d) str s = "Hello";
Answer: a) String s = "Hello";
Question 18: How do you find the length of a string s in Java?
a) [Link]()
b) [Link]()
c) length(s)
d) len(s)
Answer: b) [Link]()
Object-Oriented Programming
Question 19: What is a class in Java?
a) A collection of functions
b) A data type
c) A keyword
d) A reserved word
Answer: b) A data type
4
Question 20: Which access modifier makes a member accessible only within its own package
and by subclasses?
a) public
b) protected
c) private
d) package-private
Answer: d) package-private
Question 21: How do you define a constructor for a class Car in Java?
a) void Car() { ... }
b) Car() { ... }
c) constructor Car() { ... }
d) Car() { ... }
Answer: d) Car() { ... }
Inheritance and Polymorphism
Question 22: What is inheritance in Java?
a) A way to hide data members
b) A way to derive a new class from an existing class
c) A way to create multiple objects
d) A way to override methods
Answer: b) A way to derive a new class from an existing class
Question 23: Which keyword is used to implement inheritance in Java?
a) inherit
b) extends
c) inherits
d) class
Answer: b) extends
Question 24: What is polymorphism in Java?
a) A way to perform multiple operations
b) A way to perform division
c) A way to call methods
d) A way to define multiple methods with the same name
5
Answer: d) A way to define multiple methods with the same name
Exception Handling
Question 25: What is exception handling in Java?
a) A way to handle errors that occur during program execution
b) A way to handle memory allocation
c) A way to handle input/output operations
d) A way to handle inheritance
Answer: a) A way to handle errors that occur during program execution
Question 26: Which keyword is used to throw an exception in Java?
a) try
b) throw
c) catch
d) exception
Answer: b) throw
Question 27: What is the purpose of the catch block in exception handling in Java?
a) To throw an exception
b) To handle the caught exception
c) To terminate the program
d) To define a function
Answer: b) To handle the caught exception
Collections Framework
Question 28: What is the Java Collections Framework?
a) A way to handle errors
b) A set of pre-defined data structures
c) A way to define functions
d) A way to perform I/O operations
Answer: b) A set of pre-defined data structures
Question 29: Which interface is used to implement a dynamic array in Java?
a) List
b) Set
6
c) Map
d) Array
Answer: a) List
Question 30: How do you iterate through elements in a List in Java using iterators?
a) for (Iterator it = [Link](); [Link](); ) { ... }
b) for (Iterator it = [Link](); [Link](); ) { ... }
c) for (Iterator it = [Link](); [Link](); ) { ... }
d) for (Iterator it = [Link](); [Link](); ) { ... }
Answer: a) for (Iterator it = [Link](); [Link](); ) { ... }
File Handling
Question 31: Which class is used for file handling in Java?
a) File
b) Files
c) FileWriter
d) FileReader
Answer: a) File
Question 32: What is the correct way to open a file named [Link] for writing in Java? a)
File file = new File("[Link]");
b) FileWriter file = new FileWriter("[Link]");
c) FileWriter file = new FileWriter("[Link]", true);
d) FileWriter file = new FileWriter("[Link]", false);
Answer: c) FileWriter file = new FileWriter("[Link]", true);
Generics
Question 33: What are generics in Java?
a) A way to define multiple classes
b) A way to define multiple methods
c) A way to create generic classes or methods
d) A way to declare multiple variables
Answer: c) A way to create generic classes or methods
Question 34: How do you define a generic class in Java?
7
a) class MyGeneric<T> { ... }
b) class MyGeneric { ... }
c) generic class MyGeneric<T> { ... }
d) class MyGeneric<T> extends Object { ... }
Answer: a) class MyGeneric<T> { ... }
Memory Management
Question 35: How is memory allocated for objects in Java?
a) Automatically by the garbage collector
b) Explicitly using malloc()
c) Using the new keyword
d) Both a and c
Answer: d) Both a and c
Question 36: How do you free memory allocated to an object in Java?
a) Java handles memory automatically
b) Using delete keyword
c) Using free() function
d) Manually set to null
Answer: a) Java handles memory automatically
Miscellaneous
Question 37: What is the purpose of the static keyword in Java?
a) To create global variables
b) To define constants
c) To make methods or variables belong to the class rather than instances
d) To prevent inheritance
Answer: c) To make methods or variables belong to the class rather than instances
Question 38: What is the purpose of the super keyword in Java?
a) To call the superclass constructor
b) To access superclass methods and variables
c) To declare a superclass
d) To override a superclass method
Answer: b) To access superclass methods and variables
8
Question 39: Which method is called when an object is no longer referenced?
a) delete()
b) destroy()
c) finalize()
d) dispose()
Answer: c) finalize()
Question 40: What does the this keyword refer to in Java?
a) Current class instance
b) Current method
c) Current package
d) Current superclass
Answer: a) Current class instance
Standard Library
Question 41: Which class is used to perform input/output operations in Java?
a) IO
b) File
c) Scanner
d) System
Answer: c) Scanner
Question 42: How do you print to the console in Java?
a) print()
b) [Link]()
c) [Link]()
d) [Link]()
Answer: d) [Link]()
Question 43: Which method is used to convert a string to an integer in Java?
a) toInt()
b) parseInt()
c) convert()
d) stringToInt()
Answer: b) parseInt()
9
Question 44: What is the purpose of the toString() method in Java?
a) To convert objects to strings
b) To print objects
c) To concatenate strings
d) To compare strings
Answer: a) To convert objects to strings
Threads
Question 45: What is a thread in Java?
a) A lightweight process
b) A data structure
c) A method
d) A class
Answer: a) A lightweight process
Question 46: Which interface is used to create threads in Java?
a) Thread
b) Runnable
c) Process
d) Task
Answer: b) Runnable
Question 47: How do you start a thread in Java?
a) [Link]()
b) start(thread)
c) start()
d) run(thread)
Answer: a) [Link]()
Serialization
Question 48: What is serialization in Java?
a) A way to convert objects into byte stream
b) A way to print objects
c) A way to create threads
d) A way to handle exceptions
10
Answer: a) A way to convert objects into byte stream
Question 49: Which interface is used for serialization in Java?
a) Serializable
b) Serialize
c) SerializeObject
d) SerializeInterface
Answer: a) Serializable
Annotations
Question 50: What is an annotation in Java?
a) A way to define constants
b) A way to add metadata
c) A way to create threads
d) A way to define methods
Answer: b) A way to add metadata
11