JAVA
JAVA
1. Which of the following is NOT one of the four main principles of Object Oriented Programming
(OOP)?
A) Encapsulation
B) Inheritance
C) Polymorphism
D) Compilation
Answer: D) Compilation
3. Which of the following is the correct syntax for creating an object of a class in Java?
A) `ClassName objectName = new ClassName();`
B) `objectName = ClassName();`
C) `ClassName = new objectName();`
D) `new ClassName objectName;`
Answer: A) `ClassName objectName = new ClassName();`
void display() {
System.out.println(name + " is " + age + " years old.");
}
}
class B extends A {
void display() {
System.out.println("Class B");
}
}
19. Which of the following is the correct way to implement an interface in Java?
A) `class MyClass implements MyInterface{}`
B) `class MyClass extends MyInterface{}`
C) `interface MyClass implements MyInterface{}`
D) `interface MyClass extends MyInterface{}`
Answer: A) `class MyClass implements MyInterface{}`
22. Which of the following is true about the `toString()` method in Java?
A) It is used to provide a string representation of an object
B) It must be implemented in every class
C) It returns the hashcode of the object
D) It is automatically called when printing an object
Answer: A) It is used to provide a string representation of an object
();
}
}
```
A) Test
B) NullPointerException
C) Compilation error
D) Runtime error
Answer: B) NullPointerException
25. Which of the following is the correct way to handle exceptions in Java?
A) `try { ... } catch(Exception e) { ... }`
B) `try { ... } finally { ... }`
C) `catch(Exception e) { ... }`
D) `try { ... } exception { ... }`
Answer: A) `try { ... } catch(Exception e) { ... }`
5. Which version of Java introduced the concept of "Write Once, Run Anywhere"?
A) Java 1.0
B) Java 5
C) Java 7
D) Java 8
Answer: A) Java 1.0
14. Which of the following Java buzzwords means the language hides unnecessary details from the
user?
A) Simplicity
B) Object Oriented
C) Encapsulation
D) Robustness
Answer: A) Simplicity
15. What is meant by the term "Multithreaded" in Java?
A) Running multiple applications at the same time
B) Running multiple processes within the same application
C) Having a single process with many threads of execution
D) Running on multiple platforms simultaneously
Answer: C) Having a single process with many threads of execution
18. Which Java version introduced Lambda expressions and Streams API?
A) Java 5
B) Java 6
C) Java 7
D) Java 8
Answer: D) Java 8
19. Which of the following buzzwords reflects Java's capability to interact with other languages and
platforms?
A) Distributed
B) Dynamic
C) Secure
D) Portable
Answer: B) Dynamic
22. Which of the following buzzwords refers to Java's ability to be modified and extended easily?
A) Distributed
B) Dynamic
C) Secure
D) Portable
Answer: B) Dynamic
5. Which of the following memory areas is shared by all threads in a Java application?
A) Heap
B) Stack
C) Method Area
D) Program Counter Register
Answer: A) Heap
7. Which of the following registers holds the address of the current instruction being executed in
JVM?
A) Program Counter Register (PC)
B) Stack Pointer
C) Execution Register
D) Address Register
Answer: A) Program Counter Register (PC)
13. What happens when the JVM's Garbage Collector detects an unused object?
A) It releases the memory and removes the object from the heap
B) It moves the object to another memory area
C) It stores the object in a cache
D) It keeps the object for future use
Answer: A) It releases the memory and removes the object from the heap
14. Which of the following is true about the Method Area in JVM?
A) It stores local variables
B) It stores class metadata, method data, and static variables
C) It stores bytecode for execution
D) It stores objects created by classes
Answer: B) It stores class metadata, method data, and static variables
16. Which part of JVM is responsible for method invocation and return?
A) Stack
B) Heap
C) Method Area
D) Program Counter
Answer: A) Stack
20. What is the role of the Native Method Interface (JNI) in JVM?
A) It allows Java code to interface with native applications written in other languages
B) It compiles Java source code into bytecode
C) It manages memory for native objects
D) It handles garbage collection for native memory
Answer: A) It allows Java code to interface with native applications written in other languages
21. Where are method calls and local variables stored in JVM?
A) Heap
B) Method Area
C) Stack
D) Program Counter Register
Answer: C) Stack
23. Which memory area is used by the JVM to store references to objects created in Java?
A) Stack
B) Heap
C) Method Area
D) Program Counter Register
Answer: B) Heap
2. Which of the following data types can store a decimal number in Java?
A) int
B) float
C) boolean
D) char
Answer: B) float
4. Which of the following data types can store a true/false value in Java?
A) int
B) char
C) boolean
D) float
Answer: C) boolean
6. Which of the following is the correct way to declare a character variable in Java?
A) char ch = 'A';
B) char ch = "A";
C) char ch = A;
D) character ch = 'A';
Answer: A) char ch = 'A';
7. Which data type would you use to store a large decimal number in Java?
A) double
B) int
C) char
D) boolean
Answer: A) double
9. Which data type can store the largest integer value in Java?
A) int
B) long
C) double
D) byte
Answer: B) long
10. Which of the following is the default value of an `int` variable in Java?
A) 0
B) 1
C) null
D) false
Answer: A) 0
12. Which of the following is NOT a valid Java primitive data type?
A) int
B) boolean
C) String
D) byte
Answer: C) String
13. How much memory does the `int` data type take up in Java?
A) 2 bytes
B) 4 bytes
C) 8 bytes
D) 16 bytes
Answer: B) 4 bytes
14. What is the maximum value a `byte` variable can store in Java?
A) 128
B) 255
C) 32767
D) 2147483647
Answer: A) 128
18. Which of the following is the correct declaration of a `double` variable in Java?
A) double num = 3.14;
B) double num = 3.14f;
C) double num = 3.14d;
D) double num = 3;
Answer: A) double num = 3.14;
19. Which of the following is the default value of an instance variable of type `String` in Java?
A) null
B) empty string
C) "undefined"
D) "null"
Answer: A) null
20. What will be the value of an uninitialized local `int` variable in Java?
A) 0
B) null
C) Compilation error
D) Garbage value
Answer: C) Compilation error
21. Which of the following data types can store the smallest range of values?
A) int
B) byte
C) long
D) double
Answer: B) byte
22. What is the correct way to declare a float variable with a value of 5.75 in Java?
A) float f = 5.75;
B) float f = 5.75f;
C) float f = 5.75F;
D) Both B and C
Answer: D) Both B and C
23. Which of the following is used to store large integer values in Java?
A) int
B) long
C) double
D) BigInteger
Answer: D) BigInteger
24. What is the result of the following operation: `int x = 10 / 3;` in Java?
A) 3.333
B) 3
C) 3.0
D) Compilation error
Answer: B) 3
25. Which of the following data types can be assigned a value of `true` or `false` in Java?
A) int
B) boolean
C) String
D) char
Answer: B) boolean
Here are 25 MCQ questions with answers on Variables in Java:
9. What is the scope of a class level variable (also known as an instance variable)?
A) It can be accessed within the method only.
B) It can be accessed anywhere in the class.
C) It can be accessed in other classes.
D) It cannot be accessed at all.
Answer: B) It can be accessed anywhere in the class.
13. Can you declare two variables with the same name in Java?
A) Yes, if they are in different methods.
B) Yes, if they are of different data types.
C) No, variable names must be unique in a class.
D) Yes, if one is static and the other is non static.
Answer: C) No, variable names must be unique in a class.
15. What is the default value of an instance variable of type `char` in Java?
A) 0
B) '\0'
C) 'a'
D) null
Answer: B) '\0' (Null character)
19. What type of variable can be accessed directly from a class without creating an object in Java?
A) Instance variable
B) Static variable
C) Local variable
D) Constant variable
Answer: B) Static variable
21. Which type of variable is declared with the `final` keyword in Java?
A) Local variable
B) Constant variable
C) Static variable
D) Instance variable
Answer: B) Constant variable
22. What is the main purpose of the `static` keyword in Java when applied to variables?
A) To make the variable constant
B) To make the variable accessible across all methods
C) To share the variable between all instances of the class
D) To make the variable accessible in other classes
Answer: C) To share the variable between all instances of the class
23. Which of the following is NOT a valid variable type modifier in Java?
A) static
B) final
C) public
D) dynamic
Answer: D) dynamic
24. What is the default value of an instance variable of type `Object` in Java?
A) null
B) 0
C) false
D) undefined
Answer: A) null
10. What happens when a local variable goes out of scope in Java?
A) It is automatically deleted from memory.
B) Its value is reset to its default value.
C) Its value is lost, but it is not deleted until garbage collection.
D) The program will throw a runtime error.
Answer: A) It is automatically deleted from memory.
15. Which of the following can hold the longest lived value in a Java program?
A) Local variables
B) Instance variables
C) Static variables
D) Constructor parameters
Answer: C) Static variables
19. Can a local variable be accessed outside the method where it is declared?
A) Yes, if it is assigned to an instance variable.
B) No, local variables are only accessible within the method.
C) Yes, if it is a static variable.
D) Yes, if it is returned from the method.
Answer: B) No, local variables are only accessible within the method.
22. Which of the following is true about the lifetime of a local variable in Java?
A) It is created when the method starts and destroyed when the method ends.
B) It is created when the class is loaded and destroyed when the class is unloaded.
C) It remains in memory as long as the program runs.
D) It is created when an object is created and destroyed when the object is destroyed.
Answer: A) It is created when the method starts and destroyed when the method ends.
23. Which keyword is used to declare a variable that cannot be reassigned in Java?
A) static
B) final
C) const
D) immutable
Answer: B) final
cannot be modified.
C) Yes, but only by the class constructor.
D) Yes, only if it is an instance variable.
Answer: A) Yes, as long as it is not declared `final`.
3. Which of the following is the correct way to access the first element of an array in Java?
A) `arr[1]`
B) `arr[0]`
C) `arr.first()`
D) `arr.get(1)`
Answer: B) `arr[0]`
11. Which of the following is the correct way to declare and initialize an array in Java?
A) `int arr = {1, 2, 3};`
B) `int[] arr = new int[3] {1, 2, 3};`
C) `int[] arr = {1, 2, 3};`
D) `int[] arr = new int[3]; arr = {1, 2, 3};`
Answer: C) `int[] arr = {1, 2, 3};`
12. Which statement correctly accesses the last element of the array `arr` in Java?
A) `arr[arr.length];`
B) `arr[arr.length 1];`
C) `arr[arr.length + 1];`
D) `arr[arr.length 2];`
Answer: B) `arr[arr.length 1];`
18. What is the correct way to copy the contents of one array to another in Java?
A) `arr.copy(arr2);`
B) `arr = arr2;`
C) `System.arraycopy(arr, 0, arr2, 0, arr.length);`
D) `arr.copyOf(arr2);`
Answer: C) `System.arraycopy(arr, 0, arr2, 0, arr.length);`
19. Which of the following is the correct way to create a 2D array with 3 rows and 2 columns in Java?
A) `int[][] arr = new int[3][2];`
B) `int[][] arr = new int[3, 2];`
C) `int[][] arr = new int[2][3];`
D) `int[3, 2] arr = new int[][];`
Answer: A) `int[][] arr = new int[3][2];`
];`
D) All of the above
Answer: D) All of the above
24. How can you find the index of an element in an array in Java?
A) Use `indexOf()` method.
B) Use `Arrays.indexOf()` method.
C) Use `Arrays.binarySearch()` method.
D) Use `find()` method.
Answer: C) Use `Arrays.binarySearch()` method.
7. Which of the following is the correct syntax for the ternary operator in Java?
A) `condition ? expression1 : expression2`
B) `condition ? expression2 : expression1`
C) `condition ? expression1 : (expression2)`
D) None of the above
Answer: A) `condition ? expression1 : expression2`
12. Which of the following is the correct usage of the `&&` (AND) operator in Java?
A) `true && false`
B) `false && false`
C) `true && true`
D) All of the above
Answer: D) All of the above
18. Which of the following is the correct result of the following expression:
`5 + 3 * 2 1`?
A) 10
B) 12
C) 9
D) 11
Answer: C) 9
22. What is the correct syntax for using the bitwise OR operator in Java?
A) `|`
B) `&&`
C) `^`
D) `!!`
Answer: A) `|`
6. Which of the following is the correct syntax for a `for` loop in Java?
A) `for (int i = 0; i < 10; i++)`
B) `for (i = 0, i < 10, i++)`
C) `for (int i = 0; i < 10)`
D) `for (int i = 0; i++)`
Answer: A) `for (int i = 0; i < 10; i++)`
10. Which of the following is the correct way to use the `continue` statement in Java?
A) `continue;`
B) `continue loop;`
C) `continue to next;`
D) `continue next;`
Answer: A) `continue;`
11. Which control structure in Java allows you to choose between multiple options?
A) `if`
B) `switch`
C) `while`
D) `for`
Answer: B) `switch`
12. How does a `switch` statement differ from an `if else` statement?
A) `switch` allows you to check multiple conditions without repetition.
B) `switch` can only be used with numeric data types.
C) `switch` supports logical conditions, unlike `if else`.
D) `switch` is faster than `if else` for all types of conditions.
Answer: A) `switch` allows you to check multiple conditions without repetition.
16. Which statement in Java can be used to transfer control to the beginning of a loop?
A) `continue`
B) `break`
C) `return`
D) `exit`
Answer: A) `continue`
18. How many times will the following code print "Hello"?
`int i = 0;
do {
System.out.println("Hello");
i++;
} while (i < 3);`
A) 1
B) 2
C) 3
D) Infinite loop
Answer: C) 3
20. Which of the following control statements does not need an expression or condition?
A) `if`
B) `for`
C) `switch`
D) `break`
Answer: D) `break`
22. What is the condition for the `while` loop to terminate in Java?
A) The condition becomes `true`.
B) The condition becomes `false`.
C) The loop will run indefinitely unless `break` is used.
D) Both A and C
Answer: B) The condition becomes `false`.
23. Which of the following statements is used to skip the current iteration of a loop in Java?
A) `exit`
B) `continue`
C) `break`
D) `return`
Answer: B) `continue`
else`
25. Which of the following is true about the `for` loop in Java?
A) It is used when the number of iterations is known.
B) It can only be used with numeric types.
C) It must always have a condition to terminate.
D) It can only loop through arrays.
Answer: A) It is used when the number of iterations is known.
Here are 25 MCQ questions with answers on Type Conversion and Casting in Java :
2. What is the term for converting a larger data type to a smaller one?
A) Implicit Casting
B) Explicit Casting
C) Downcasting
D) Upcasting
Answer: C) Downcasting
3. What happens if you try to assign a `double` to an `int` variable without casting?
A) The value is automatically converted to `int`.
B) A compilation error occurs.
C) The value is rounded down.
D) The value is truncated.
Answer: B) A compilation error occurs.
4. Which type of casting is used when you convert a smaller data type to a larger data type?
A) Implicit Casting
B) Explicit Casting
C) Downcasting
D) Narrowing Conversion
Answer: A) Implicit Casting
12. What happens when you try to assign a larger type value to a smaller type without casting?
A) The value is automatically truncated.
B) The value is rounded.
C) A compilation error occurs.
D) The program runs but produces incorrect results.
Answer: C) A compilation error occurs.
15. Which of the following is the correct order of casting in Java from smallest to largest data types?
A) `byte > short > int > long > float > double`
B) `double > float > long > int > short > byte`
C) `short > byte > int > float > double > long`
D) `byte > long > int > double > float`
Answer: A) `byte > short > int > long > float > double`
22. Which of the following is true about casting between primitive data types in Java?
A) Casting from a smaller data type to a larger one is always done implicitly.
B) You cannot cast between primitive types in Java.
C) Casting from a larger data type to a smaller one may result in data loss.
D) Both A and C are correct.
Answer: D) Both A and C are correct.
23. What happens when you cast a larger numeric type to a smaller one?
A) You lose the precision and can get unexpected results.
B) The result is rounded up to the nearest integer.
C) No change occurs.
D) A compilation error occurs.
Answer: A) You lose the precision and can get unexpected results.
24. How do you convert a `String` to an `int` in Java?
A) `int x = String.valueOf("10");`
B) `int x = (int) "10";`
C) `int x = Integer.parseInt("10");`
D) `int x = Integer.toString(
10);`
Answer: C) `int x = Integer.parseInt("10");`
25. Which of the following statements is correct about type casting in Java?
A) Implicit casting is used for narrowing conversions.
B) Explicit casting is used for widening conversions.
C) Widening conversions do not lose data.
D) Narrowing conversions are automatically handled by Java.
Answer: C) Widening conversions do not lose data.
Here are 25 MCQ questions with answers on Constructors in Java :
11. Which of the following constructor is called when a subclass object is created?
A) The default constructor of the subclass
B) The parameterized constructor of the subclass
C) The constructor of the superclass is always called first.
D) The constructor of the subclass is called first.
Answer: C) The constructor of the superclass is always called first.
13. Which constructor is invoked when an object of a class is created with parameters?
A) The default constructor
B) The parameterized constructor
C) A copy constructor
D) No constructor is invoked.
Answer: B) The parameterized constructor
15. What will happen if a class has a constructor with parameters and no default constructor is
provided?
A) The class will not compile.
B) The default constructor is automatically provided.
C) The class will compile, but you must use the constructor with parameters.
D) The program will crash at runtime.
Answer: C) The class will compile, but you must use the constructor with parameters.
20. What is the difference between a default constructor and a parameterized constructor?
A) A default constructor takes no arguments, while a parameterized constructor takes arguments.
B) A default constructor is used to initialize variables, while a parameterized constructor is not.
C) A default constructor must always be provided.
D) A parameterized constructor initializes default values, while a default constructor does not.
Answer: A) A default constructor takes no arguments, while a parameterized constructor takes
arguments.
23. What happens if you explicitly invoke a constructor from a superclass in a subclass constructor?
A) The subclass constructor will be skipped.
B) The superclass constructor will be called first, followed by the subclass constructor.
C) The subclass constructor will be called first, followed by the superclass constructor.
D) It causes a compilation error.
Answer: B) The superclass constructor will be called first, followed by the subclass constructor.
6. In Java, what is the return type of a method that does not return any value?
A) `void`
B) `null`
C) `Object`
D) `empty`
Answer: A) `void`
12. Which of the following can be used to call a static method in Java?
A) `object.method()`
B) `Class.method()`
C) `this.method()`
D) `super.method()`
Answer: B) `Class.method()`
14. What is the keyword used to invoke the method of the parent class in Java?
A) `this`
B) `super`
C) `call`
D) `parent`
Answer: B) `super`
19. What will happen if a method is called with incorrect arguments in Java?
A) A compilation error will occur.
B) The program will execute, but return an unexpected value.
C) A runtime exception will occur.
D) The method will return `null`.
Answer: A) A compilation error will occur.
22. What is the default value of a method's argument if it is not provided in Java?
A) `null`
B) The method argument is required and cannot be left empty.
C) Default values for arguments like `0` for integers.
D) The argument remains uninitialized.
Answer: B) The method argument is required and cannot be left empty.
24. What will happen if a non static method is called from a static context in Java?
A) The non static method will be executed normally.
B) Compilation error occurs.
C) A runtime error will occur.
D) The static context will be ignored.
Answer: B) Compilation error occurs.
25. Which of the following statements is true about method parameters in Java?
A) Parameters can be passed by reference only.
B) Parameters can be passed by value or reference.
C) All parameters are passed by reference.
D) Parameters are always passed by value.
Answer: D) Parameters are always passed
by value.
Here are 25 MCQ questions with answers on Static Block and Static Data in Java:
14. What will happen if you try to access a static variable before the class is loaded into memory?
A) Compilation error
B) Runtime error
C) It will work as long as the class is loaded at some point
D) The static variable will return `null`
Answer: B) Runtime error
15. Which of the following statements is correct about static blocks and static variables in Java?
A) Static blocks are used to initialize static variables.
B) Static blocks can only access instance variables.
C) Static variables cannot be initialized in a static block.
D) Static variables are initialized when the object is created.
Answer: A) Static blocks are used to initialize static variables.
17. What is the result of accessing a static variable using an object reference?
A) It throws a `NullPointerException`.
B) The static variable is accessed as a normal instance variable.
C) The variable can still be accessed but it's not recommended.
D) Compilation error occurs.
Answer: C) The variable can still be accessed but it's not recommended.
18. What is the default value of a static variable if not initialized?
A) `0` for numbers, `false` for booleans, and `null` for objects
B) `null` for all types of static variables
C) `undefined` for all types of static variables
D) No default value; it will cause a runtime error.
Answer: A) `0` for numbers, `false` for booleans, and `null` for objects
20. Which of the following is true about the order of execution of static blocks in Java?
A) Static blocks are executed in the reverse order in which they are written.
B) Static blocks are executed in the order in which they are written in the class.
C) Static blocks are executed after the main method.
D) Static blocks are never executed.
Answer: B) Static blocks are executed in the order in which they are written in the class.
: A) Yes, exceptions can be handled in a static block using `try catch` blocks.
3. Which of the following is the correct syntax for calling a static method in Java?
A) `methodName();`
B) `Class.methodName();`
C) `this.methodName();`
D) `new Class().methodName();`
Answer: B) `Class.methodName();`
6. What is the result of calling the `length()` method on a String object in Java?
A) It returns the memory address of the string.
B) It returns the total number of characters in the string.
C) It returns the number of words in the string.
D) It returns the index of the last character.
Answer: B) It returns the total number of characters in the string.
9. Which of the following is the correct way to create a String object in Java?
A) `String s = "Hello";`
B) `String s = new String("Hello");`
C) `String s = new String();`
D) All of the above.
Answer: D) All of the above.
13. Which of the following methods is used to replace characters in a string in Java?
A) `replace()`
B) `change()`
C) `swap()`
D) `substitute()`
Answer: A) `replace()`
19. Which method of the StringBuffer class is used to reverse the characters in a string?
A) `reverse()`
B) `flip()`
C) `invert()`
D) `reverseString()`
Answer: A) `reverse()`
25. How can you convert a StringBuffer object to a String object in Java?
A) `sb.toString()`
B) `String.valueOf(sb)`
C) `sb.convert()`
D) Both A and B are correct.
Answer: D) Both A and B are correct.
Below are 25 MCQs for each topic you mentioned in Java:
14. What happens if the `final` keyword is used with a method in Java?
A) The method can be overridden.
B) The method cannot be overridden.
C) The method will be abstract.
D) The method is deprecated.
Answer: B) The method cannot be overridden.
16. What will happen if a subclass does not implement all abstract methods of a parent class?
A) Compilation error
B) Runtime exception
C) No error will occur
D) The subclass will be abstract
Answer: A) Compilation error.
17. Which of the following is true for dynamic method dispatch in Java?
A) It refers to the method call resolution at compile time.
B) It refers to the method call resolution at runtime.
C) It works only with static methods.
D) None of the above.
Answer: B) It refers to the method call resolution at runtime.
18. In which of the following situations is the `final` keyword used in Java?
A) To define a constant variable
B) To prevent method overriding
C) To prevent inheritance
D) All of the above
Answer: D) All of the above.
20. Which access modifier is used by default for class members in Java?
A) public
B) private
C) protected
D) package private (no modifier)
Answer: D) package private (no modifier).
22. What is the significance of the `super()` constructor call in a subclass constructor?
A) It invokes a constructor of the immediate parent class.
B) It invokes the constructor of the grandparent class.
C) It prevents the subclass constructor from being invoked.
D) It is unnecessary in a subclass constructor.
Answer: A) It invokes a constructor of the immediate parent class.
23. What does the `instanceof` keyword do in Java?
A) It checks if an object is an instance of a class.
B) It creates an instance of a class.
C) It invokes a method from a class.
D) It checks for method overriding.
Answer: A) It checks if an object is an instance of a class.
25. Which of the following is the correct statement about inheritance in Java?
A) A child class can inherit from multiple classes.
B) A child class can inherit from only one class.
C) A class can inherit from another class and implement an interface.
D) Both B and C are correct.
Answer: D) Both B and C are correct.
Here are 25 MCQs with answers for each topic you mentioned: Multithreaded Programming and
I/O Streams in Java.
### Multithreaded Programming: Thread Class, Runnable Interface, Synchronization, Deadlock, etc.
10. Which of the following is the correct way to use the `synchronized` keyword on a block of code?
A) `synchronized { code }`
B) `synchronized(code) { }`
C) `synchronized(object) { code }`
D) `synchronized code { }`
Answer: C) `synchronized(object) { code }`
14. Which of the following classes in Java can be used to create a thread?
A) `Thread`
B) `Runnable`
C) Both A and B
D) `ExecutorService`
Answer: C) Both A and B
22. Which interface does a class need to implement to create a thread in Java?
A) `Thread`
B) `Runnable`
C) `Executor`
D) `Callable`
Answer: B) `Runnable`
24. Which method allows one thread to wait for another to finish in Java?
A) `join()`
B) `wait()`
C) `notify()`
D) `interrupt()`
Answer: A) `join()`
3. Which of the following is the root class for all input/output streams in Java?
A) DataStream
B) IOStream
C) InputStream and OutputStream
D) FileStream
Answer: C) InputStream and OutputStream
8. Which class is used for buffering output data for faster writing?
A) BufferedReader
B) BufferedWriter
C) PrintWriter
D) FileWriter
Answer: B) BufferedWriter
10. Which of the following classes provides methods to read objects from a file?
A) ObjectOutputStream
B) ObjectInputStream
C) FileOutputStream
D) FileInputStream
Answer: B) ObjectInputStream
18. Which stream is used to read and write data to a file in Java?
A) DataInputStream and DataOutputStream
B) FileInputStream and FileOutputStream
C) ObjectInputStream and ObjectOutputStream
D) BufferedReader and BufferedWriter
Answer: B) FileInputStream and FileOutputStream
21. Which method is used to read a single byte from a file in Java?
A) readByte()
B) readChar()
C) read()
D) nextByte()
Answer: C) read()
22. What is the purpose of `BufferedReader` in Java?
A) To write data to a file efficiently
B) To read data efficiently from a file or console
C) To read binary data from a file
D) To perform object serialization
Answer: B) To read data efficiently from a file or console
24. How do you handle exceptions while reading from a file in Java?
A) By using the `catch` block
B) By using the `throws` keyword
C) By using the `finally` block
D) All of the above
Answer: D) All of the above