wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CMDP2063 Unix and C Programming (Revision)

Total questions: 15

Worksheet time: 30mins

Name
Class
Date
1.

What does the fopen() function do when opening a file in "r" mode?

a)

Opens a file for both reading and writing

b)

Opens a file for appending data to it

c)

Opens a file for reading only

d)

Creates a new file for writing

2.

What is the primary difference between printf() and fprintf() in C?

a)

fprintf() is used for reading input from a file, while printf() writes to a file.

b)

fprintf() is used for file output, while printf() is used for console output.

c)

fprintf() is slower than printf() because it works with larger files.

d)

fprintf() writes output to the console, while printf() writes to files.

3.

Which of the following is the correct use of fgets() to read a line of text from a file?

a)

fgets(str, INPUT_SIZE, f);

b)

fgets(str, INPUT_SIZE, "input.txt");

c)

fgets(str, 100, "file.txt");

d)

fgets("text.txt", 100, str);

4.

In the following code, which of the following is the correct way to access the month field of the nested struct?

b)

answer->date.month;

c)

answer[0].month;

5.

After allocating memory for a struct named door with malloc(), which function should be used to deallocate the memory?

a)

delete(door);

b)

free(door);

c)

destroy(door);

d)

deallocate(door);

6.

Which of the following is a valid initialization of a Result struct?

a)

Result res = {75, "B"};

b)

Result res = {'B', 75};

c)

Result res = {75, 'B'};

d)

Result res = {grade = 'B', mark = 75};

7.

What is the primary purpose of a debugger?

a)

To automatically fix errors in code

b)

To compile and run code more efficiently.

c)

To optimize code performance.

d)

To help debug software by allowing users to inspect and control a running program.

8.

How can you add the -g flag in a Makefile to include debugging information during compilation?

a)

CFLAGS = -Wall -pedantic -ansi -g

b)

CFLAGS = -debug

c)

CFLAGS = -g

d)

CFLAGS = -O2

9.

What does the file permission rwxr-x--- imply for "everyone else"?

a)

They have read and execute permissions.

b)

They have no permissions.

c)

They have read-only permissions.

d)

They have write and execute permissions.

10.

Which of the following is the primary purpose of the -l switch in the ls command?

a)

To display detailed information about files, including permissions, ownership, and type

b)

To list files in reverse order

c)

To show hidden files only

d)

To list files in alphabetical order

11.

What is required before using rand() to generate random numbers?

a)

Calling time(NULL)

b)

Calling srand() with a seed value

c)

Initializing a random number generator

d)

Declaring an integer variable

12.

What will the following code print?

a)

Three identical numbers

b)

A single random number repeated three times

c)

Three random numbers

d)

The current time three times

13.

If var = 32, what is the result of var >> 2?

a)

16

b)

4

c)

8

d)

32

14.

What is the behavior of the following script?

a)

Checks if myfile is a directory.

b)

Checks if myfile is an empty file.

c)

Checks if myfile contains the text "exists!".

d)

Checks if myfile exists and is a regular file.

15.

What is the purpose of the print command in GDB?

a)

To execute a program step-by-step.

b)

To print the stack trace.

c)

To set a breakpoint in the program.

d)

To display the value of a variable or expression.