C Programming Hardest Questions Test! Trivia

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Poojajanga
P
Poojajanga
Community Contributor
Quizzes Created: 1 | Total Attempts: 230
| Attempts: 230 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");

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".

Submit
Please wait...
About This Quiz
C Programming Hardest Questions Test! Trivia - Quiz

Dive into the complexities of C programming with this challenging trivia! Test your understanding of file operations, string handling, and more. Perfect for enhancing programming skills and preparing for advanced C programming tasks.

Personalize your quiz and earn a certificate with your name on it!
2. 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;

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".

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

float a;
double 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.

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

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.

Submit
5. What does fp point to in the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    return 0;
}

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.

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

Explanation

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

Submit
7. 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;
}

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.

Submit
8. 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;
}

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.

Submit
9. 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;
}

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.

Submit
10. 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?

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".

Submit
View My Results

Quiz Review Timeline (Updated): Nov 16, 2023 +

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
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following operations can be performed on the file...
To print out a and b given below, which of the...
To scan a and b given below, which of the...
Out of fgets() and gets() which function is safe...
What does fp point to in the program? ...
What is the purpose...
Which files will get closed through the fclose() in the...
Consider the following program and what will be content of t?...
On executing the below program what will be the contents of...
In a file contains the line "I am a boy\r\n" then on reading...
Alert!

Advertisement