Unit 5 - Practice Quiz

CSE325 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which header file must be included to use POSIX threads in a C program?

A. <pthread.h>
B. <sys/thread.h>
C. <thread.h>
D. <process.h>

2 Which compiler flag is typically required to compile a multithreaded program using gcc on Linux?

A. -kthread
B. -pthread
C. -thread
D. -lpthread

3 What is the return type of the function pthread_create?

A. void *
B. void
C. pthread_t
D. int

4 In the function pthread_create, what is the purpose of the fourth argument?

A. It stores the thread ID.
B. It is the function pointer to the thread routine.
C. It is the argument passed to the start_routine.
D. It sets the thread attributes.

5 Which function is used to wait for the termination of a specific thread?

A. pthread_exit()
B. pthread_stop()
C. pthread_join()
D. pthread_wait()

6 What happens if pthread_exit() is called in the main thread of a process, while other threads are still running?

A. The function returns an error.
B. The main thread waits for all other threads to finish.
C. The main thread terminates, but other threads continue execution.
D. The entire process terminates immediately.

7 Which function returns the thread ID of the calling thread?

A. pthread_self()
B. getpid()
C. pthread_getid()
D. pthread_id()

8 Which data type is used to identify a thread in POSIX?

A. pthread_t
B. int
C. void *
D. struct thread

9 Why is pthread_equal(id1, id2) used instead of the == operator?

A. Because == is slower.
B. Because pthread_t is a float.
C. Because pthread_t is an opaque data type and may be a structure.
D. Because thread IDs are always strings.

10 What is a Race Condition?

A. A mechanism to speed up thread execution.
B. A situation where a thread waits indefinitely for a resource.
C. A condition where threads race to finish execution first.
D. A situation where multiple threads access shared data concurrently and the outcome depends on execution order.

11 Which of the following is NOT a requirement for a solution to the Critical Section Problem?

A. Progress
B. Bounded Waiting
C. Mutual Exclusion
D. First-In-First-Out (FIFO) Execution

12 A segment of code where shared resources are accessed is called a:

A. Atomic region
B. Race condition
C. Mutex
D. Critical section

13 If a variable is incremented () by two threads concurrently without synchronization, what is the problem?

A. Deadlock
B. Segmentation Fault
C. Starvation
D. Lost Update

14 Which POSIX function initializes a mutex?

A. pthread_init_mutex()
B. pthread_mutex_start()
C. pthread_mutex_init()
D. pthread_mutex_create()

15 What is the correct macro to statically initialize a mutex?

A. PTHREAD_MUTEX_INITIALIZER
B. MUTEX_INIT
C. PTHREAD_MUTEX_INIT
D. STATIC_MUTEX

16 What happens if a thread calls pthread_mutex_lock() on a mutex that is already locked by another thread?

A. The calling thread is terminated.
B. The calling thread returns an error immediately.
C. The calling thread blocks (sleeps) until the mutex is unlocked.
D. The calling thread steals the lock.

17 Which function attempts to lock a mutex but returns immediately if it is already locked?

A. pthread_mutex_test()
B. pthread_mutex_trylock()
C. pthread_mutex_lock_nowait()
D. pthread_mutex_check()

18 When a thread is finished with a critical section protected by a mutex, it must call:

A. pthread_mutex_unlock()
B. pthread_mutex_release()
C. pthread_mutex_destroy()
D. pthread_mutex_stop()

19 What is a binary semaphore effectively similar to?

A. A monitor
B. A mutex
C. A spinlock
D. A condition variable

20 Which header file is required for using POSIX semaphores?

A. <pthread.h>
B. <sys/sem.h>
C. <semaphore.h>
D. <sem.h>

21 What is the data type for a POSIX semaphore?

A. pthread_mutex_t
B. mutex_t
C. sem_t
D. semaphore

22 Which function is used to initialize an unnamed semaphore?

A. sem_init()
B. sem_open()
C. sem_create()
D. sem_start()

23 In sem_init(sem_t *sem, int pshared, unsigned int value), what does pshared = 0 imply?

A. The semaphore is shared only between threads of the same process.
B. The semaphore value is initialized to 0.
C. The semaphore is shared between processes.
D. The semaphore is disabled.

24 The sem_wait() function corresponds to which theoretical semaphore operation?

A. P operation (Proberen/Wait)
B. V operation (Signal)
C. Init operation
D. Destroy operation

25 The sem_post() function corresponds to which theoretical semaphore operation?

A. V operation (Verhogen/Signal)
B. Lock operation
C. Test operation
D. P operation (Wait)

26 If a semaphore value is 0, what does sem_wait() do?

A. It returns an error.
B. It destroys the semaphore.
C. It blocks (waits) until the value becomes greater than 0.
D. It increments the value to 1.

27 What is the primary difference between pthread_mutex_lock and sem_wait on a binary semaphore?

A. Mutexes can be used between processes, semaphores cannot.
B. Mutexes can only be unlocked by the thread that locked them; Semaphores can be signaled by any thread.
C. Semaphores are faster.
D. There is no difference.

28 Consider a Counting Semaphore initialized to . If 5 threads call sem_wait() almost simultaneously, how many will immediately proceed?

A. 1
B. 3
C. 5
D. 0

29 Which function is used to free the resources associated with a mutex?

A. pthread_mutex_delete()
B. free()
C. pthread_mutex_kill()
D. pthread_mutex_destroy()

30 Which function allows a thread to separate itself from the main thread, such that its resources are automatically released upon termination?

A. pthread_split()
B. pthread_detach()
C. pthread_release()
D. pthread_free()

31 In the Producer-Consumer problem, what is the purpose of the mutex?

A. To signal when the buffer is empty.
B. To ensure exclusive access to the buffer when adding or removing items.
C. To count the number of items.
D. To signal when the buffer is full.

32 Which of the following creates a "Deadlock"?

A. High CPU usage by a thread.
B. Two threads waiting for a resource held by the other.
C. A thread sleeping for 10 seconds.
D. A thread waiting for a semaphore initialized to 0.

33 What is the return value of sem_trywait() if the semaphore is currently 0?

A. -1 and sets errno to EINTR.
B. -1 and sets errno to EAGAIN.
C. 0
D. It blocks until available.

34 To pass multiple arguments to a thread function, one should:

A. Pass them as separate arguments to pthread_create.
B. Define a structure containing the arguments and pass a pointer to it.
C. Use global variables only.
D. Cast them to integers.

35 What is the danger of passing a pointer to a local variable (on the stack) to a thread created via pthread_create?

A. It is perfectly safe.
B. The creating function might return and deallocate the stack before the thread reads the variable.
C. The thread cannot access stack memory.
D. It causes a compile error.

36 Which of the following is correct regarding User-level threads vs Kernel-level threads?

A. User-level threads are managed by the OS kernel.
B. Blocking one user-level thread never blocks the entire process.
C. POSIX threads on Linux are purely user-level threads.
D. Context switching is faster in User-level threads.

37 What is a Spinlock?

A. A lock that rotates between threads.
B. A hard disk locking mechanism.
C. A lock where the waiting thread sleeps.
D. A lock where the waiting thread loops (spins) checking the lock condition.

38 If you want to synchronize threads based on a condition (e.g., 'Wait until Queue is not empty'), which mechanism is best suited alongside a mutex?

A. Global Boolean Flag
B. Another Mutex
C. Spinlock
D. Condition Variables (pthread_cond_t)

39 Which function signals a condition variable, waking up at least one waiting thread?

A. pthread_cond_broadcast()
B. pthread_cond_wait()
C. pthread_cond_init()
D. pthread_cond_signal()

40 Why must pthread_cond_wait() be called inside a loop checking the condition?

A. Because the syntax requires it.
B. It creates a spinlock.
C. It is not required, an if statement is sufficient.
D. To handle 'spurious wakeups'.

41 When pthread_cond_wait(&cond, &mutex) is called, what happens to the mutex?

A. The mutex is destroyed.
B. The mutex is passed to the next thread.
C. The mutex is automatically released while the thread waits.
D. The mutex remains locked by the thread.

42 What is the maximum value a binary semaphore can hold?

A. 1
B. Depending on OS
C. 0
D. Infinity

43 Can pthread_join be called on a detached thread?

A. Yes, but it returns immediately.
B. Only if the thread has finished.
C. No, it results in undefined behavior or error.
D. Yes, always.

44 In a multithreaded program, which of the following is shared among all threads?

A. Program Counter
B. Stack Pointer
C. Register Set
D. Global Heap Memory

45 If a thread attempts to unlock a mutex that it does not own, what is the result?

A. The thread gets ownership.
B. Undefined behavior (usually an error).
C. The mutex becomes unlocked regardless of ownership.
D. It works successfully.

46 Which of the following is thread-safe?

A. Using a global variable as a counter without locks.
B. Using strtok_r (reentrant version).
C. Using strtok (standard static version).
D. Modifying a linked list simultaneously without locks.

47 What is the concept of Priority Inversion?

A. A low-priority thread runs faster than a high-priority one.
B. A high-priority thread waits for a lower-priority thread holding a lock.
C. All threads have the same priority.
D. The scheduler assigns priorities randomly.

48 How do Named Semaphores (sem_open) differ from Unnamed Semaphores (sem_init)?

A. Named semaphores are identified by a string name and can be used by unrelated processes.
B. Named semaphores are faster.
C. There is no difference.
D. Unnamed semaphores use strings for identification.

49 Which function closes a named semaphore?

A. sem_exit()
B. sem_destroy()
C. sem_end()
D. sem_close()

50 What happens if you free() a structure that contains an active mutex?

A. Undefined behavior; the mutex should be destroyed first.
B. The OS cleans it up safely.
C. The mutex is automatically destroyed.
D. The program crashes immediately.