Hack4Hack

Operating Systems practice questions with answers

Practise operating system questions at your own pace: processes, threads, scheduling, deadlocks, memory, paging and synchronisation. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.

  1. What is the main difference between a process and a thread?

    easy

    1. Threads cannot run in parallel
    2. Threads of the same process share its memory; processes have separate address spaces
    3. Processes are always faster
    4. There is no difference

    Answer: B. Threads of the same process share its memory; processes have separate address spaces

    Explanation: Sharing memory makes threads cheap to create and communicate, but they need synchronisation to avoid races.

  2. What is a context switch?

    easy

    1. Changing the user interface theme
    2. Saving the state of the running process or thread and loading another's so the CPU can run it
    3. Switching between programming languages
    4. Restarting the OS

    Answer: B. Saving the state of the running process or thread and loading another's so the CPU can run it

    Explanation: Context switches allow multitasking but cost time (saving registers, switching memory maps, cold caches).

  3. Which scheduling algorithm can cause starvation of long jobs?

    easy

    1. First come, first served
    2. Shortest job first
    3. Round robin
    4. None

    Answer: B. Shortest job first

    Explanation: If short jobs keep arriving, a long job may never run. Ageing (raising priority over time) prevents this.

  4. What is virtual memory?

    easy

    1. Memory in the cloud
    2. An abstraction giving each process its own address space, mapped to RAM or disk by the OS and hardware
    3. Cache memory
    4. A RAM disk

    Answer: B. An abstraction giving each process its own address space, mapped to RAM or disk by the OS and hardware

    Explanation: It isolates processes and lets programs use more memory than physical RAM, with pages moved to and from disk as needed.

  5. Which four conditions must all hold for a deadlock?

    medium

    1. Mutual exclusion, hold and wait, no preemption, circular wait
    2. Starvation, ageing, paging, swapping
    3. Mutual exclusion, preemption, priority, polling
    4. Race, lock, signal, wait

    Answer: A. Mutual exclusion, hold and wait, no preemption, circular wait

    Explanation: These are the Coffman conditions. Breaking any one prevents deadlock, for example by acquiring locks in a fixed order to stop circular wait.

  6. What is thrashing?

    medium

    1. Overheating of the CPU
    2. The system spends most of its time swapping pages in and out instead of doing work
    3. A disk failure
    4. A type of malware

    Answer: B. The system spends most of its time swapping pages in and out instead of doing work

    Explanation: It happens when the working sets of running processes do not fit in RAM. Reduce the number of processes or add memory.

  7. What is the difference between a mutex and a semaphore?

    medium

    1. They are identical
    2. A mutex lets one owner hold the lock; a counting semaphore allows up to N concurrent holders
    3. A semaphore only works between processes
    4. A mutex is slower

    Answer: B. A mutex lets one owner hold the lock; a counting semaphore allows up to N concurrent holders

    Explanation: A binary semaphore looks like a mutex, but a mutex has an owner who must release it; a semaphore can be signalled by any thread.

  8. With 3 page frames and FIFO replacement, how many page faults does the reference string 1, 2, 3, 4, 1, 2 cause?

    medium

    1. 4
    2. 5
    3. 6
    4. 3

    Answer: C. 6

    Explanation: 1, 2, 3 fault (cold). 4 faults and evicts 1; 1 faults and evicts 2; 2 faults and evicts 3: 6 faults in total.

  9. What is a race condition?

    hard

    1. Two programs competing for the fastest CPU
    2. The result depends on the unpredictable timing of threads accessing shared data without proper synchronisation
    3. A scheduling algorithm
    4. A network timeout

    Answer: B. The result depends on the unpredictable timing of threads accessing shared data without proper synchronisation

    Explanation: counter++ is read, add, write: two threads can read the same value and one update is lost. Use locks or atomic operations.

  10. What is Belady's anomaly?

    hard

    1. More RAM always means fewer faults
    2. With FIFO page replacement, adding page frames can increase the number of page faults
    3. LRU can be slower than FIFO
    4. Caches are useless for random access

    Answer: B. With FIFO page replacement, adding page frames can increase the number of page faults

    Explanation: Stack algorithms such as LRU and the optimal policy never show this anomaly; FIFO can.

  11. What does the TLB (translation lookaside buffer) do?

    hard

    1. Stores files
    2. Caches recent virtual-to-physical page translations so most memory accesses skip a page-table walk
    3. Balances network load
    4. Schedules threads

    Answer: B. Caches recent virtual-to-physical page translations so most memory accesses skip a page-table walk

    Explanation: A TLB miss means walking the page table in memory. Huge pages reduce misses for programs with large memory footprints.

  12. Which of these happen in user mode vs kernel mode? (Select the ones that need kernel mode)

    hard

    1. Reading a file from disk
    2. Adding two numbers in a loop
    3. Allocating a new memory page from the OS
    4. Sending data over a network socket

    Answer: A. Reading a file from disk; C. Allocating a new memory page from the OS; D. Sending data over a network socket

    Explanation: Hardware access and memory management go through system calls into the kernel. Plain arithmetic runs entirely in user mode.

Play the Daily Quiz at 4 PM IST