Unit 5 - Practice Quiz

CSE310

1 Which of the following packages contains the classes and interfaces required for Input and Output operations in Java?

A. java.util
B. java.io
C. java.lang
D. java.awt

2 In Java I/O, what is the abstract base class for all byte stream classes used for reading data?

A. java.io.InputStream
B. java.io.Reader
C. java.io.FileInputStream
D. java.io.Input

3 Which of the following classes is the abstract base class for writing character streams?

A. java.io.OutputStream
B. java.io.Writer
C. java.io.FileWriter
D. java.io.CharacterStream

4 What is the primary difference between Byte Streams and Character Streams in Java?

A. Byte streams read 16-bit data, while Character streams read 8-bit data.
B. Byte streams are used for text files, while Character streams are used for images.
C. Byte streams process data byte by byte (8-bit), while Character streams process data character by character (16-bit Unicode).
D. There is no functional difference; they are interchangeable.

5 Which standard stream in Java corresponds to the standard error output device?

A. System.in
B. System.out
C. System.err
D. System.exit

6 What is the result of applying the try-with-resources statement on a stream object that implements AutoCloseable?

A. The stream is automatically flushed but not closed.
B. The stream is automatically closed when the try block is exited.
C. The stream remains open until close() is explicitly called.
D. It forces the garbage collector to run immediately.

7 Which class would you use to read primitive Java data types (like int, double, boolean) from an InputStream in a portable way?

A. ObjectInputStream
B. DataInputStream
C. BufferedInputStream
D. FileInputStream

8 Why is it generally recommended to wrap a FileDescriptor or stream with a BufferedReader or BufferedInputStream?

A. To encrypt the data being read.
B. To convert byte data into character data.
C. To improve I/O performance by reducing the number of physical disk accesses.
D. To allow serialization of objects.

9 What does the java.io.File class represent?

A. The contents of a file.
B. An abstract representation of file and directory pathnames.
C. A stream to read binary data.
D. A buffer for file operations.

10 Which method is used to force any buffered output bytes to be written out to the underlying stream?

A. write()
B. clear()
C. flush()
D. dump()

11 Which exception is thrown if a file cannot be found during an attempt to open it with FileInputStream?

A. IOException
B. FileNotFoundException
C. FileLockedException
D. EOFException

12 When reading a file using FileInputStream, what value is returned by the read() method when the end of the file is reached?

A. $0$
B. $1$
C.
D. null

13 Which interface must a class implement to allow its objects to be written to a stream via Serialization?

A. java.io.Serialized
B. java.io.Serializable
C. java.io.Externalizable
D. java.lang.Cloneable

14 Which keyword is used to prevent a specific field from being serialized?

A. static
B. volatile
C. transient
D. private

15 Which class is used to deserialize an object from a stream?

A. ObjectOutputStream
B. ObjectInputStream
C. DataInputStream
D. StreamReader

16 What is the purpose of serialVersionUID in Serialization?

A. It determines the priority of the thread serializing the object.
B. It counts the number of objects serialized.
C. It is a version control identifier to ensure the sender and receiver have compatible classes.
D. It is a security token for encryption.

17 If a parent class implements Serializable, what is the status of its subclasses?

A. Subclasses are automatically Serializable.
B. Subclasses must explicitly implement Serializable.
C. Subclasses cannot be serialized.
D. Subclasses throw a NotSerializableException.

18 Which syntax correctly defines a custom generic class named Box that works with any type ?

A. public class Box<T> { ... }
B. public class Box(T) { ... }
C. public class Box[T] { ... }
D. public class Box {T} { ... }

19 What is the Diamond Operator introduced in Java 7?

A. The operator :: used for method references.
B. The operator <> used to infer type arguments during instantiation.
C. The operator -> used in Lambda expressions.
D. The operator ? used in ternary operations.

20 Consider the declaration: class MathBox<T extends Number>. What kind of parameter is ?

A. Unbounded type parameter
B. Lower bounded type parameter
C. Upper bounded type parameter
D. Wildcard parameter

21 Which wildcard expression represents an Upper Bounded Wildcard that accepts any type that is a subclass of Number?

A. <? super Number>
B. <? extends Number>
C. <?>
D. <T extends Number>

22 What does the Lower Bounded Wildcard <? super Integer> accept?

A. Only the Integer class.
B. Any class that is a subclass of Integer.
C. Any class that is a superclass of Integer (or Integer itself).
D. Any class except Integer.

23 What is Type Erasure in Java Generics?

A. The process of removing unused variables during compilation.
B. The process where the compiler replaces generic types with their bounds (or Object) in the bytecode.
C. The runtime deletion of generic objects.
D. The ability to use primitive types in generics.

24 Why can you not instantiate a generic type directly like new T()?

A. Because is not a valid class name.
B. Because the constructor of is always private.
C. Because of Type Erasure, the type of is not known at runtime.
D. Because generics only support static methods.

25 Which of the following is valid usage of generics?

A. List<int> numbers = new ArrayList<>();
B. List<Object> numbers = new ArrayList<String>();
C. List<Integer> numbers = new ArrayList<>();
D. static T instanceVar;

26 Which symbol represents an Unbounded Wildcard?

A. *
B. ?
C. T
D. U

27 To create a generic method, where is the type parameter (e.g., <T>) declared?

A. After the method name.
B. Inside the method body.
C. Before the return type.
D. After the return type.

28 What is the main benefit of using Generics in Java?

A. Faster runtime execution.
B. Compile-time type safety and elimination of explicit casts.
C. Support for multiple inheritance.
D. Ability to use pointers.

29 Which two ways can be used to create a new thread of execution in Java?

A. Extending Thread class or Implementing Runnable interface.
B. Extending Runnable class or Implementing Thread interface.
C. Importing java.thread or Using System.exec().
D. Using ProcessBuilder or Runtime.exec().

30 Which method is called to begin the execution of a thread?

A. init()
B. start()
C. run()
D. execute()

31 What happens if you call the run() method directly instead of start()?

A. A new thread is created.
B. A RuntimeException is thrown.
C. The code executes in the current thread (no multithreading).
D. The program crashes.

32 Which interface contains the run() method?

A. Callable
B. Runnable
C. Executor
D. Cloneable

33 What is the state of a thread after it is created but before start() is called?

A. Runnable
B. New
C. Blocked
D. Terminated

34 Which method causes the currently executing thread to pause execution for a specified period?

A. Thread.yield()
B. Thread.stop()
C. Thread.sleep(long millis)
D. object.wait()

35 What is the purpose of the join() method?

A. To connect two threads to share data.
B. To allow one thread to wait for the completion of another.
C. To merge two thread stacks.
D. To start a thread immediately.

36 What is the range of thread priorities in Java?

A. $0$ to $10$
B. $1$ to $10$
C. $1$ to $100$
D. $0$ to $1$

37 What is a Daemon Thread?

A. A thread with the highest priority.
B. A service provider thread that runs in the background and terminates when all user threads finish.
C. A thread that cannot be stopped.
D. A thread used for heavy calculations.

38 Which keyword is used to prevent multiple threads from accessing a block of code or method simultaneously?

A. volatile
B. synchronized
C. static
D. transient

39 When a thread enters a synchronized instance method, which lock does it acquire?

A. The lock of the class (Class object).
B. The lock of the current object instance (this).
C. The lock of the Main thread.
D. No lock is acquired.

40 In which class are the methods wait(), notify(), and notifyAll() defined?

A. java.lang.Thread
B. java.lang.Object
C. java.lang.Runnable
D. java.util.concurrent.Lock

41 What happens if a thread calls wait() on an object without holding that object's lock (synchronization)?

A. The thread waits indefinitely.
B. It throws an IllegalMonitorStateException.
C. It throws an InterruptedException.
D. It works normally.

42 What is the difference between notify() and notifyAll()?

A. notify() wakes up all threads, notifyAll() wakes up one.
B. notify() wakes up a single thread waiting on the monitor, notifyAll() wakes up all threads waiting on the monitor.
C. notify() is for static methods, notifyAll() is for instance methods.
D. There is no difference.

43 What describes a Deadlock situation?

A. A thread loops infinitely.
B. Two or more threads are blocked forever, each waiting on the other to release a resource.
C. A thread terminates unexpectedly.
D. The JVM runs out of memory.

44 What is the purpose of the volatile keyword?

A. To make a variable constant.
B. To ensure the value of a variable is always read from main memory, not from a thread's local cache.
C. To serialize a variable.
D. To make a method synchronized.

45 Which method suggests to the thread scheduler that the current thread is willing to yield its current use of a processor?

A. sleep()
B. yield()
C. wait()
D. stop()

46 When a static synchronized method is executed, what lock is acquired?

A. The lock of the current object instance.
B. The lock associated with the Class object.
C. A random lock.
D. No lock is required.

47 Which exception must be handled (or declared) when using Thread.sleep()?

A. IOException
B. InterruptedException
C. TimeoutException
D. NullPointerException

48 What is a Race Condition?

A. A competition between threads to get higher priority.
B. A situation where the outcome of a program depends on the unpredictable timing or interleaving of multiple threads.
C. A condition where a thread runs faster than expected.
D. The condition needed to start a thread.

49 Which method checks if a thread is still executing?

A. checkAccess()
B. isAlive()
C. activeCount()
D. getState()

50 When using the Scanner class to read input, which method reads a full line of text?

A. next()
B. nextInt()
C. nextLine()
D. read()