Unit 5 - Practice Quiz
CSE310
1 Which of the following packages contains the classes and interfaces required for Input and Output operations in Java?
java.util
java.io
java.lang
java.awt
2 In Java I/O, what is the abstract base class for all byte stream classes used for reading data?
java.io.InputStream
java.io.Reader
java.io.FileInputStream
java.io.Input
3 Which of the following classes is the abstract base class for writing character streams?
java.io.OutputStream
java.io.Writer
java.io.FileWriter
java.io.CharacterStream
4 What is the primary difference between Byte Streams and Character Streams in Java?
5 Which standard stream in Java corresponds to the standard error output device?
System.in
System.out
System.err
System.exit
6
What is the result of applying the try-with-resources statement on a stream object that implements AutoCloseable?
close() is explicitly called.
7
Which class would you use to read primitive Java data types (like int, double, boolean) from an InputStream in a portable way?
ObjectInputStream
DataInputStream
BufferedInputStream
FileInputStream
8
Why is it generally recommended to wrap a FileDescriptor or stream with a BufferedReader or BufferedInputStream?
9
What does the java.io.File class represent?
10 Which method is used to force any buffered output bytes to be written out to the underlying stream?
write()
clear()
flush()
dump()
11
Which exception is thrown if a file cannot be found during an attempt to open it with FileInputStream?
IOException
FileNotFoundException
FileLockedException
EOFException
12
When reading a file using FileInputStream, what value is returned by the read() method when the end of the file is reached?
null
13 Which interface must a class implement to allow its objects to be written to a stream via Serialization?
java.io.Serialized
java.io.Serializable
java.io.Externalizable
java.lang.Cloneable
14 Which keyword is used to prevent a specific field from being serialized?
static
volatile
transient
private
15 Which class is used to deserialize an object from a stream?
ObjectOutputStream
ObjectInputStream
DataInputStream
StreamReader
16
What is the purpose of serialVersionUID in Serialization?
17
If a parent class implements Serializable, what is the status of its subclasses?
Serializable.
Serializable.
NotSerializableException.
18
Which syntax correctly defines a custom generic class named Box that works with any type ?
public class Box<T> { ... }
public class Box(T) { ... }
public class Box[T] { ... }
public class Box {T} { ... }
19 What is the Diamond Operator introduced in Java 7?
:: used for method references.
<> used to infer type arguments during instantiation.
-> used in Lambda expressions.
? used in ternary operations.
20
Consider the declaration: class MathBox<T extends Number>. What kind of parameter is ?
21
Which wildcard expression represents an Upper Bounded Wildcard that accepts any type that is a subclass of Number?
<? super Number>
<? extends Number>
<?>
<T extends Number>
22
What does the Lower Bounded Wildcard <? super Integer> accept?
Integer class.
Integer.
Integer (or Integer itself).
Integer.
23 What is Type Erasure in Java Generics?
24
Why can you not instantiate a generic type directly like new T()?
25 Which of the following is valid usage of generics?
List<int> numbers = new ArrayList<>();
List<Object> numbers = new ArrayList<String>();
List<Integer> numbers = new ArrayList<>();
static T instanceVar;
26 Which symbol represents an Unbounded Wildcard?
*
?
T
U
27
To create a generic method, where is the type parameter (e.g., <T>) declared?
28 What is the main benefit of using Generics in Java?
29 Which two ways can be used to create a new thread of execution in Java?
Thread class or Implementing Runnable interface.
Runnable class or Implementing Thread interface.
java.thread or Using System.exec().
ProcessBuilder or Runtime.exec().
30 Which method is called to begin the execution of a thread?
init()
start()
run()
execute()
31
What happens if you call the run() method directly instead of start()?
RuntimeException is thrown.
32
Which interface contains the run() method?
Callable
Runnable
Executor
Cloneable
33
What is the state of a thread after it is created but before start() is called?
34 Which method causes the currently executing thread to pause execution for a specified period?
Thread.yield()
Thread.stop()
Thread.sleep(long millis)
object.wait()
35
What is the purpose of the join() method?
36 What is the range of thread priorities in Java?
37 What is a Daemon Thread?
38 Which keyword is used to prevent multiple threads from accessing a block of code or method simultaneously?
volatile
synchronized
static
transient
39
When a thread enters a synchronized instance method, which lock does it acquire?
Class object).
this).
Main thread.
40
In which class are the methods wait(), notify(), and notifyAll() defined?
java.lang.Thread
java.lang.Object
java.lang.Runnable
java.util.concurrent.Lock
41
What happens if a thread calls wait() on an object without holding that object's lock (synchronization)?
IllegalMonitorStateException.
InterruptedException.
42
What is the difference between notify() and notifyAll()?
notify() wakes up all threads, notifyAll() wakes up one.
notify() wakes up a single thread waiting on the monitor, notifyAll() wakes up all threads waiting on the monitor.
notify() is for static methods, notifyAll() is for instance methods.
43 What describes a Deadlock situation?
44
What is the purpose of the volatile keyword?
45 Which method suggests to the thread scheduler that the current thread is willing to yield its current use of a processor?
sleep()
yield()
wait()
stop()
46 When a static synchronized method is executed, what lock is acquired?
Class object.
47
Which exception must be handled (or declared) when using Thread.sleep()?
IOException
InterruptedException
TimeoutException
NullPointerException
48 What is a Race Condition?
49 Which method checks if a thread is still executing?
checkAccess()
isAlive()
activeCount()
getState()
50
When using the Scanner class to read input, which method reads a full line of text?
next()
nextInt()
nextLine()
read()