KEMBAR78
Module 5 Question Bank CPII (FILE Handling) | PDF | Pointer (Computer Programming) | Text File
0% found this document useful (0 votes)
8 views24 pages

Module 5 Question Bank CPII (FILE Handling)

The document contains multiple-choice questions (MCQs) related to file handling and command line arguments in C programming, organized by different professors. Key topics include file modes, functions for reading and writing files, and the use of command line arguments. Each question is followed by the correct answer, providing a comprehensive review of essential concepts in C programming.

Uploaded by

shreyasbr771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Module 5 Question Bank CPII (FILE Handling)

The document contains multiple-choice questions (MCQs) related to file handling and command line arguments in C programming, organized by different professors. Key topics include file modes, functions for reading and writing files, and the use of command line arguments. Each question is followed by the correct answer, providing a comprehensive review of essential concepts in C programming.

Uploaded by

shreyasbr771
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

C Programming II with Linux - Practice MCQs

Module 5 ( FILE Handling and Command Line Arguments)

Prof. Manjunath
1 What does the mode "r" mean when opening a file with fopen()?
a) Open for writing only
b) Open for reading only
c) Open for both reading and writing
d) Open for appending
Answer: b

2 What will fopen("sample.txt", "w") do if the file already exists?


a) Append to the file
b) Open without modifying
c) Truncate the file to zero length and open for writing
d) Open for reading
Answer: c

3 What value does fopen() return on failure?


a) 0
b) -1
c) NULL
d) EOF
Answer: c

4
Which function is used to write a string to a file?

a) fputs()

b) fwrite()

c) fprintf()

d) All of the above

Answer: d

5
What is the purpose of fseek() in C?
a) To search in a file

b) To close a file

c) To move the file pointer

d) To rename a file

Answer: c

6 What does the "b" in file mode "rb" stand for?


a) Backup
b) Binary
c) Block
d) Bitwise
Answer: b

7
Which of the following modes opens a file for both reading and
writing, and creates it if it doesn’t exist?

a) "r"

b) "w"

c) "w+"

d) "a"

Answer: c
8
What is the difference between fgetc() and fgets()?

a) fgetc() reads a string; fgets() reads a character

b) fgetc() reads one character; fgets() reads a line

c) No difference

d) fgets() is for binary files only


Answer: b
9
What happens if you access argv[argc]?

a) You get the last argument​


b) Compilation error​
c) You access a NULL pointer​
d) You get the first argument

Answer: c) You access a NULL pointer. argv[argc] is a NULL pointer


used to mark the end of the arguments.

10
Which of the following actions is allowed in all these fopen() modes: "r", "r+",
"w+", and "a+"? (Assume file is existing)

a) Read and write​


b) Read​
c) Append and write​
d) Write

Answer: b)Read.

11
Interpret the code snippet and choose the correct option

int x=atoi("3.14");

printf("%d",x);

a)​ Compilation error


b)​ Gives warning and prints output 3
c)​ Prints 3 without error and warnings
d)​ Prints 3.14

Answer : c)

12
What will be the output of the program when run as “./a.out 5 10.9” ?
#include <stdio.h>

#include <stdlib.h>

int main(int argc, char *argv[]) {

int a = atoi(argv[1]);

int b = atoi(argv[2]);

printf("%d\n", a + b);

return 0;

a)​ Compilation error


b)​ 15.9
c)​ 16
d)​ 15

Answer d) 15

Prof. Pradeep

1 Which mode is used to open a file for both reading and writing in binary mode?
A. "rw"
B. "rb+"
C. "wr"
D. "br+"
Answer: B. "rb+"

2 Which function is used to read a single character from a file?


A. fscanf()
B. fgetc()
C. freadchar()
D. getchar()
Answer: B. fgetc()
3
To read a string from a file until a newline or EOF is encountered, we use:

A. fscanf()

B. fgets()

C. getchar()

D. freadline()

Answer: B. fgets()

4
Which of the following modes is not valid in fopen()?

A. "r+b"

B. "rb+"

C. "rw"

D. "a+"

Answer: C. "rw"

5 What is the purpose of this code?#include <stdio.h>

int main() {
FILE *fp = fopen("test.txt", "r");
char ch;
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
}
fclose(fp);
return 0;
}

A. Write characters to a file​


B. Read and display characters one by one​
C. Append data to the file​
D. Copy file content

Answer: B. Read and display characters one by one

6 What does fgets() do in this context?#include <stdio.h>

int main() {
FILE *fp = fopen("test.txt", "r");
char buffer[50];
while (fgets(buffer, 50, fp)) {
printf("%s", buffer);
}
fclose(fp);
return 0;
}

A. Reads a character​
B. Reads one line up to 49 chars​
C. Reads the whole file​
D. Reads one line up to 50 chars

Answer: B. Reads one line up to 49 chars

7 What is the correct function signature to use command line arguments in C?


A. int main()
B. int main(char *argv[], int argc)
C. int main(int argc, char *argv[])
D. int main(char argc, char *argv[])
Answer: C. int main(int argc, char *argv[])

8 What is stored in argv[0]?


A. Data or path of the program
B. NULL
C. Name or path of the program
D. Value or path of the program
Answer: C. Name or path of the program

9 What is the type of argv?


A. char **
B. int *
C. char *
D. void *
Answer: A. char **

10 What is printed by the following code if run with: ./a.out arg1 arg2?
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%s", argv[2]);
return 0;
}

A. ./a.out
B. arg1
C. arg2
D. Segmentation fault
Answer: C. arg2

Prof. Gourish

1 What does fgetc() return at the end of a file?


A. 0
B. NULL
C. EOF
D. -1

✅ Answer: C. EOF
2 Which function is used to close a file?
A. endfile()
B. close()
C. fclose()
D. stopfile()

✅ Answer: C. fclose()
3 What type is the first parameter of fopen()?
A. char
B. int
C. FILE *
D. const char *
✅ Answer: D. const char *
4 Which function reads a line from a file into a string?
A. freadline()
B. readline()
C. fgets()
D. getline()

✅ Answer: C. fgets()
5 Which mode is used to append data to a file without truncating it?
A. "w"
B. "a"
C. "r+"
D. "w+"

✅ Answer: B. "a"
6 What type is argv in int main(int argc, char *argv[])?
A. char[]
B. char *
C. char **
D. int *

✅ Answer: C. char **
7 What is the output of fseek(fp, 0, SEEK_END); followed by ftell(fp);?
A. Beginning of file
B. 0
C. End of file position (in bytes)
D. NULL

✅ Answer: C. End of file position (in bytes)


8 Which loop construct is most appropriate when copying character-by-character till
EOF?
A. while ((ch = fgetc(fp)) != EOF)
B. for (ch = fgetc(fp); !feof(fp); ch = fgetc(fp))
C. do...while (ch != EOF)
D. while (ch != NULL)

✅ Answer: A. while ((ch = fgetc(fp)) != EOF)


9 What does argc represent in the function header int main(int argc, char *argv[])?
A. Argument character
B. Number of command-line arguments
C. Argument string
D. Argument array

✅ Answer: B. Number of command-line arguments


10 Which header file is needed to use atoi() and atof() functions?
A. stdio.h
B. math.h
C. string.h
D. stdlib.h

✅ Answer: D. stdlib.h
11 What does atoi("123") return?
A. "123"
B. 123.0
C. 123
D. Compilation error

✅ Answer: C. 123
12 What does atof("3.14") return?
A. "3.14"
B. 3.14 as a float
C. 3.14 as a double
D. Compilation error

✅ Answer: C. 3.14 as a double


13 If a user runs the program like this: ./prog 10 20, what is the value of argc?
A. 0
B. 1
C. 2
D. 3

✅ Answer: D. 3
(Explanation: Includes prog name + two arguments)

14 What will argv[1] contain in the program ./program 10 20?


A. NULL
B. ./program
C. 10
D. 20
✅ Answer: C. 10
15 What happens if the user runs the program with no command-line arguments (e.g.,
./program)?
A. argc will be 0
B. argc will be 1, and argv[0] will contain the program name
C. The program will crash
D. argv[1] will be NULL

✅ Answer: B. argc will be 1, and argv[0] will contain the program name
16 What will happen if argv is accessed with an index greater than or equal to argc?
A. It will return a random value
B. It will throw an error
C. It will return NULL
D. It will print garbage values

✅ Answer: C. It will return NULL


Prof. Pratik

1
Which of the following modes would be appropriate if it is required to write to an
existing file without clearing it?
A. “r+”
B. “w”
C. “a+”
D. “r”

Solution: C

2
Which of the following functions would be more appropriate for reading an entire
line from a file?
A. fline()
B. fscanf()
C. fgetc()
D. fgets()

Solution: D

3
Which of the following is true about file pointers?
A. Pointer arithmetic is also applicable to file pointers
B. It is a pointer to a structure called FILE
C. It is also called a file position indicator
D. Once initialized, stores the address of the file’s data

Solution: B

4
Which of the following is the correct call to fgetc( )? Here ‘fp’ is a pointer and ‘ch’
is a character.
A. ch=fgetc(fp);
B. ch=fgetc( );
C. fp=fgetc(ch);
D. fp=fgetc( );

Solution: A

5
Which of the following can be used to point to ‘o’ from the second argument?

./filename hello world


A. *argv[1]
B. *argv[2]
C. *(argv[1]+4)
D. *argv[1]+4

Solution: C

6
Which of the following can be used to obtain input from the keyboard? Here ‘fp’
is a file pointer and ‘str’ is a string.
A. fgets(str,fp)
B. fgets(str, 20)
C. fgets(str, “keyboard”)
D. fgets(str, 20, stdin);

Solution: D

7
Which of the following is the correct call to fputs( )? Here ‘fp’ is a file pointer, ‘ch’
is a character and ‘str’ is a string.
A. fputs(ch,fp)
B. fputs(str,fp)
C. fputs(fp,str)
D. fputs(fp,ch)

Solution: B

8
Which of the following is true about the end of file (EOF) character?
A. It is not a physical character at the end of a file
B. Operating system sends EOF signal to the program
C. It can also be invoked from keyboard using CTRL-D
D. All of the above
Solution: D

9
Which of the following is true about the exit( ) function?
A. Defined in ctype.h
B. Terminate a loop and proceed to next statement
C. exit(0) would indicate execution failure
D. Terminate program execution

Solution: D

Prof. Sowmya.B

1
What is the correct syntax to open a file for reading in C?

A) fopen("file.txt", "read");

B) fopen("file.txt", "r");

C) open("file.txt", "r");

D) None

Answer: B

2
What file mode is used to append data to a file?

A) "r"
B) "w"

C) "a"

D) "x"

Answer: C)

3
What is the return type of fgetc()?

A) int

B) char

C) void

D) string

Answer: A

4
What is the correct prototype of the main function to use argc and
argv?

A. void main(int argc, char argv)​


B. int main()​
C. int main(int argc, char *argv[])​
D. int main(char *argv[], int argc)

Answer: C

5
Which header file is required to use argc and argv?

A. stdio.h​
B. stdlib.h​
C. string.h​
D. None of the above

Answer: D

6
Which of the following is a valid way to access the second
command-line argument?

A. argv[1]​
B. argv(1)​
C. argc[1]​
D. *argv[1]

Answer: A

7
What does the mode "r" in fopen("file.txt", "r") signify?

A. Read and write​


B. Read only​
C. Write only​
D. Append

Answer: B
8
Which function is used to write a string to a file?

A. fread()​
B. fprintf()​
C. fwrite()​
D. fscanf()

Answer: B

9
What does argc represent in a C program?

A. The number of characters in the command line​


B. The number of arguments passed to the program, including the
program name​
C. The memory size of the arguments​
D. The return value of the main function

Answer: B

10
What is the return type of main when using argc and argv?

A. void​
B. char​
C. int​
D. float

Answer: C
Prof. Padmaja

1 What is the output of the following code.


#include <stdio.h>

int main() {
FILE *fp;
fp = fopen("example.txt", "w");
fprintf(fp, "Hello, World!");
fclose(fp);
printf("File written successfully.\n");
return 0;
}

A) The program will compile and run, creating 'example.txt' with "Hello,
World!".​
B) The program will produce a compilation error.​
C) The program will run but not create any file.​
D) The program will crash at runtime.
Answer:​
A) The program will compile and run, creating 'example.txt' with "Hello,
World!".

2 What does the following code snippet do?


#include <stdio.h>

int main() {
FILE *fp;
char str[100];

fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
while (fgets(str, 100, fp) != NULL) {
printf("%s", str);
}

fclose(fp);
return 0;
}

A) Reads and prints the entire contents of 'data.txt' line by line.​


B) Creates a new file 'data.txt'.​
C) Writes "data.txt" into the console.​
D) Deletes the file 'data.txt'.

Answer:​
A) Reads and prints the entire contents of 'data.txt' line by line.

3 Which function is used to read a single character from a file?


a) fscanf()b) fgets()c) fgetc()d) fread()
Answer: c) fgetc()

4 When does getc() returns EOF?


A) End of files is reached B)When getc() fails to read a character
C)Both of the above D)None of the above
Answer: A
5 Which one of the following is correct syntax for opening a file.
a) FILE *fopen(const *filename, const char *mode)
b) FILE *fopen(const *filename)
c) FILE *open(const *filename, const char *mode)
d) FILE open(const*filename)
Answer: a
6 Identify the error in the following code snippet:error in the foll code
snippet.
#include <stdio.h>

int main() {
FILE *fp;
int num = 100;

fp = fopen("numbers.txt", "w");
fwrite(&num, sizeof(int), 1, fp);
fclose(fp);
return 0;
}
A) Using fwrite to write an integer to a text file.​
B) Opening the file in write mode.​
C) Correct way to write data to a file.​
D) No error.

Answer:​
A) Using fwrite to write an integer to a text file.​
Note: fwrite writes binary data; for text files, fprintf is more appropriate.

7. What does the following code snippet do?


a) Writes "myfile.txt" to the console.
b) Reads and prints the content of "myfile.txt" to the console.
c) Creates a new file named "myfile.txt".
d) Deletes "myfile.txt".
Answer: b) Reads and prints the content of "myfile.txt" to the console.

8.
Which header file is required for file handling in C? a) <stdio.h>​
b) <file.h>​
c) <fstream.h>​
d) <conio.h>​
Answer: a) <stdio.h>h>

9
How can you read a line from a file in C? a) Using fgets()​
b) Using fscanf() with %s​
c) Using fread()​
d) Both a) and b)​
Answer: d) Both a) and b)b)

10 Which of the following is NOT a valid file mode in C? a) "w"


b) "rb"
c) "a"
d) "x"
Answer:d)

Prof. Chandrika A P

1 The first and second arguments of fopen() are


a)A character string containing the name of the file & the second argument is the mode.
b)A character string containing the name of the user & the second argument is the mode.
c)A character string containing file pointer & the second argument is the mode.
d)None of these
Answer:a)A character string containing the name of the file & the second argument is
the mode.
Explanation:syntax for opening a file.
FILE *fopen(const char *file_name,const char *mode)

2 A data of the file is stored in


a)RAM
b)ROM
c)Hard disk
d)None of these
Answer:c)Hard disk
Explanation:A data of the file is stored in Hard disk

3 Choose the right statement for fscanf() and scanf()


a)fscanf() can read from standard input whereas scanf() specifies a stream from which to
read
b)fscanf() can specifies a stream from which to read whereas scanf() can read only from
standard input
c)fscanf() and scanf() has no difference in their functions
d)fscanf() and scanf() can read from specified stream
Answer:b)
Explanation:fscanf() can specifies a stream from which to read whereas scanf() can
read only from standard input

4 Which files will get closed through the fclose() in the following program?
#include<stdio.h>
Void main()
{
FILE *fp,*ft;
fp=fopen(“a.txt”,”r”);
ft=fopen(“b.txt”,”r”);
fclose(fp,ft);
}
a)a,b
b)a
c)b
d)Error in fclose
Answer:d)Error in fclose
Explanation:Error occurs due to extra parameters in call to fclose() where we can pass
one parameter inside the fclose function.

5 Which mode creates a new file if the file does not exist?
a)write
b)append
c)Both write and append
d)None of these
Answer:c)Both write and append

6 Which statement will return one line from a file(file object is ‘f’)?
a)f.readline()
b)f.readlines()
c)f.read()
d)f.line()
Answer:a)f.readline()
Explanation:f.readline() will read single line at a time

7 Which of the following files Don’t need any Module/Library to be imported for
performing operations?
a)Binary Files
b)Text Files
c)CSV Files
d)None of these
Answer:b)Text Files
Explanation:for binary files will import pickle,for CSV will import CSV files but for
regular text will not import any files where we can work directly in text files.

8 What will the following code snippet print if the file contains "Hello World"?
#include <stdio.h>
int main() {
FILE *fp = fopen("myfile.txt", "r");
int count = 0;
char ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch == ' ') count++;
}
fclose(fp);
printf("%d\n", count);
return 0;
}
a) 0
b) 1
c) 2
d) 3
Answer: b) 1
Explanation:"Hello World" contains one space character.

9 What does the following code do?


#include <stdio.h>
int main() {
FILE *src = fopen("input.txt", "r");
FILE *dest = fopen("output.txt", "w");
char ch;
while ((ch = fgetc(src)) != EOF) {
fputc(ch, dest);
}
fclose(src);
fclose(dest);
return 0;
}

a) Copies the content of "output.txt" to "input.txt".


b) Copies the content of "input.txt" to "output.txt".
c) Overwrites "input.txt" with nothing.
d) Appends content to "output.txt".
Answer: b) Copies the content of "input.txt" to "output.txt".

10 What is the purpose of fprintf() in C?


a) To write formatted output to stdout.
b) To read formatted input from a file.
c) To write formatted output to a file.
d) To read formatted input from stdin.
Answer: c) To write formatted output to a file.

11 What does the following code do?


#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s sourcefile destfile\n", argv[0]);
return 1;
}
FILE *src = fopen(argv[1], "r");
FILE *dest = fopen(argv[2], "w");
char ch;
while ((ch = fgetc(src)) != EOF)
fputc(ch, dest);
fclose(src);
fclose(dest);
return 0;
}

a) Prints the contents of the source file on the screen.​


b) Copies sourcefile to destfile.​
c) Prints the command-line arguments.​
d) Counts the characters in the sourcefile.​
Answer: b) Copies sourcefile to destfile.

12 In text mode, what does the "a+" mode do?


a) Opens the file for reading only.​
b) Opens the file for writing and reading but truncates it.​
c) Opens the file for reading and appending; if the file doesn't exist, it's created.​
d) Opens the file for reading and writing from the beginning.​
Answer: c) Opens the file for reading and appending; if the file doesn't exist, it's created.
Explanation: "a+" allows both reading and appending to the file without overwriting existing
data.

13 If a program is executed as ./myprog file.txt, what does argv[0] contain?​


a) "file.txt"​
b) "myprog"​
c) 0​
d) NULL​
Answer: b) "myprog"​
Explanation: argv[0] typically holds the name of the program being run.

You might also like