C Programming Hardest Questions Test! Trivia

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Poojajanga
P
Poojajanga
Community Contributor
Quizzes Created: 1 | Total Attempts: 220
Questions: 10 | Attempts: 220

SettingsSettingsSettings
C Programming Hardest Questions Test! Trivia - Quiz

.


Questions and Answers
  • 1. 

    In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). What will str contain?

    • A.

      "I am a boy\r\n\0"

    • B.

      "I am a boy\r\0"

    • C.

      "I am a boy\n\0"

    • D.

      "I am a boy"

    Correct Answer
    C. "I am a boy\n\0"
    Explanation
    The correct answer is "I am a boy\0".

    The fgets() function reads the line from the file and stores it in the array str. It stops reading when it encounters a new line character (\n) or reaches the maximum number of characters specified. In this case, the line "I am a boy\r" is read into the array str, including the carriage return character (\r). The null character (\0) is automatically added at the end of the string to mark the end of the string. Therefore, the resulting value of str would be "I am a boy\0".

    Rate this question:

  • 2. 

    What is the purpose of "rb" in fopen() function used below in the code? FILE *fp; fp = fopen("source.txt", "rb");

    • A.

      Open "source.txt" in binary mode for reading

    • B.

      Open "source.txt" in binary mode for reading and writing

    • C.

      Create a new file "source.txt" for reading and writing

    • D.

      None of above

    Correct Answer
    A. Open "source.txt" in binary mode for reading
    Explanation
    The purpose of "rb" in the fopen() function is to open the file "source.txt" in binary mode for reading.

    Rate this question:

  • 3. 

    What does fp point to in the program? #include<stdio.h> int main() { FILE *fp; fp=fopen("trial", "r"); return 0; }

    • A.

      The first character in the file

    • B.

      A structure which contains a char pointer which points to the first character of a file.

    • C.

      The name of the file.

    • D.

      The last character in the file.

    Correct Answer
    B. A structure which contains a char pointer which points to the first character of a file.
    Explanation
    In this program, the variable 'fp' is declared as a pointer to a FILE structure. The 'fopen' function is used to open the file named "trial" in read mode. This function returns a pointer to the FILE structure that represents the opened file. Therefore, 'fp' points to a structure which contains a char pointer that points to the first character of the file.

    Rate this question:

  • 4. 

    Which of the following operations can be performed on the file "NOTES.TXT" using the below code? FILE *fp; fp = fopen("NOTES.TXT", "r+");

    • A.

      Reading

    • B.

      Writing

    • C.

      Appending

    • D.

      Read and Write

    Correct Answer
    D. Read and Write
    Explanation
    The code snippet opens the file "NOTES.TXT" with the mode "r+" which allows both reading and writing operations on the file. Therefore, the correct answer is "Read and Write".

    Rate this question:

  • 5. 

    To print out a and b given below, which of the following printf() statement will you use? #include<stdio.h> float a=3.14; double b=3.14;

    • A.

      Printf("%f %lf", a, b);

    • B.

      Printf("%Lf %f", a, b);

    • C.

      Printf("%Lf %Lf", a, b);

    • D.

      Printf("%f %Lf", a, b);

    Correct Answer
    A. Printf("%f %lf", a, b);
    Explanation
    The correct answer is printf("%f %lf", a, b). This is because the variable "a" is of type float and the variable "b" is of type double. The format specifier "%f" is used to print a float value and the format specifier "%lf" is used to print a double value. Therefore, this printf statement is the correct one to print the values of "a" and "b".

    Rate this question:

  • 6. 

    Which files will get closed through the fclose() in the following program?#include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; }

    • A.

      "A.C" "B.C" "C.C"

    • B.

      "B.C" "C.C"

    • C.

      "A.C"

    • D.

      Error in fclose()

    Correct Answer
    D. Error in fclose()
    Explanation
    The correct answer is "Error in fclose()". This is because the fclose() function only takes one file pointer as an argument, not multiple file pointers. In the given program, the fclose() function is called with three file pointers (fp, fs, ft), which is incorrect syntax and will result in an error.

    Rate this question:

  • 7. 

    On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?#include<stdio.h> int main() { int i, fss; char ch, source[20] = "source.txt", target[20]="target.txt", t; FILE *fs, *ft; fs = fopen(source, "r"); ft = fopen(target, "w"); while(1) { ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR); fputc(ch, ft); } } return 0; }

    • A.

      R n

    • B.

      Trh

    • C.

      Err

    • D.

      None of above

    Correct Answer
    B. Trh
    Explanation
    The program opens a source file named "source.txt" and a target file named "target.txt". It then reads each character from the source file using the getc() function. If the character is not the end-of-file character (EOF), it moves the file pointer of the source file forward by 4 characters using fseek(). It then writes the character to the target file using the fputc() function. This process continues until the end of the source file is reached. Therefore, the contents of the "target.txt" file will be "Trh" as the characters "T", "r", and "h" are the ones that are written to the file.

    Rate this question:

  • 8. 

    To scan a and b given below, which of the following scanf() statement will you use?#include<stdio.h> float a; double b;

    • A.

      Scanf("%f %f", &a, &b);

    • B.

      Scanf("%Lf %Lf", &a, &b);

    • C.

      Scanf("%f %Lf", &a, &b);

    • D.

      Scanf("%f %lf", &a, &b);

    Correct Answer
    D. Scanf("%f %lf", &a, &b);
    Explanation
    The correct answer is scanf("%f %lf", &a, &b) because the format specifier %f is used for scanning a float variable and %lf is used for scanning a double variable. Since a is a float and b is a double, this scanf statement will correctly scan both variables.

    Rate this question:

  • 9. 

    Out of fgets() and gets() which function is safe to use?

    • A.

      Gets()

    • B.

      Fgets()

    • C.

      None

    • D.

      Both

    Correct Answer
    B. Fgets()
    Explanation
    The correct answer is fgets(). The gets() function is considered unsafe because it does not perform any bounds checking, which means it can easily lead to buffer overflow vulnerabilities. On the other hand, fgets() allows you to specify the maximum number of characters to read, preventing buffer overflow issues. Therefore, fgets() is the safer option to use.

    Rate this question:

  • 10. 

    Consider the following program and what will be content of t?#include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; }

    • A.

      Size of "DUMMY.C" file

    • B.

      The handle associated with "DUMMY.C" file

    • C.

      Garbage value

    • D.

      Error in fileno()

    Correct Answer
    B. The handle associated with "DUMMY.C" file
    Explanation
    The fileno() function in C returns the file descriptor associated with the given file pointer. In this program, the file "DUMMY.C" is opened in write mode using the fopen() function. Then, the file descriptor of the opened file is obtained using the fileno() function and stored in the variable t. Finally, the value of t is printed, which represents the handle associated with the "DUMMY.C" file. Therefore, the content of t will be the handle associated with the "DUMMY.C" file.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Nov 16, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 27, 2016
    Quiz Created by
    Poojajanga
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.