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.
Here is this C Programming Trivia Quiz. The trivia quiz below is made up of c programming coding questions! Programming in C is fairly easy because it uses basic commands in English; this language is one of the most commonly used in a multitude of applications, including advanced scientific systems and operating systems. Refresh your understanding of this programming language by taking this short quiz. Good luck to you with this!
Questions and Answers
1.
Tell the output of this program.
void main()
{
int cnt = 5, a;
do {
a /= cnt;
} while (cnt --);
printf ("%d ", a);
}
A.
RUNTIME ERROR
B.
SYNTAX ERROR
C.
COMPILATION ERROR
D.
NO ERROR
Correct Answer
A. RUNTIME ERROR
Explanation The program will result in a runtime error. This is because the variable 'a' is not initialized before it is divided by 'cnt' in the do-while loop. Since 'a' does not have an initial value, dividing it by 'cnt' will lead to undefined behavior and a runtime error.
Rate this question:
2.
Tell the output of this program.#include
void fun(int *a, int *b)
{
int *t;
t=a;
a=b;
b=t;
}
main()
{
int a=2;
int b=3;
fun(&a,&b);
printf(%d, %d",a, b);
}
a) 3, 2 b) compilation error c) error at run time d) 2, 3 e) None of the above
A.
A
B.
B
C.
C
D.
D
E.
E
Correct Answer
D. D
Explanation The program defines a function called "fun" that takes two integer pointers as parameters. Inside the function, the values of the pointers are swapped using a temporary pointer variable. In the main function, two integer variables "a" and "b" are declared and assigned values of 2 and 3 respectively. The "fun" function is then called with the addresses of "a" and "b" as arguments. After the function call, the values of "a" and "b" remain unchanged because the swapping is done only within the function's scope. Therefore, the output of the program will be "2, 3".
Rate this question:
3.
Tell the output of the given Printf function.
printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));
Correct Answer ILOVECPROGRAM1321
EXPLAINATION:
The above printf line gives output like this because printf returns the number of character successfully written in the output.So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character.So is the output 1321.
Explanation The printf function is used to print the output to the console. In this case, the innermost printf("%s","ILOVECPROGRAM") function prints the string "ILOVECPROGRAM" and returns the number of characters written, which is 13. The next outer printf function then prints the value 13, which is of 2 characters. The next outer printf function prints the value 2, which is of 1 character. Finally, the outermost printf function prints the value 1. Therefore, the output of the given printf function is "ILOVECPROGRAM1321".
Rate this question:
4.
Deallocate memory without using free() in C.
Correct Answer void *realloc(void *ptr,size_t size);
EXPLAINATIOIN:
if size=0 then call to realloc is equivalent to free(ptr),As realloc is used to deallocate previously allocated memory and if the size=0 then it will acts as free().
Explanation The correct answer is void *realloc(void *ptr,size_t size). The realloc function in C can be used to deallocate previously allocated memory. If the size parameter is set to 0, the call to realloc is equivalent to calling free(ptr), which also deallocates the memory. Therefore, if size is 0, realloc acts as free.
Rate this question:
5.
Given is a piece of C code, whose purpose was to print a
minus sign 20 times, but are you able to notice that, it doesn't work?
#include
int main()
{
int i;
int n = 20;
for( i = 0; i < n; i-- )
printf("-");
return 0;
}
Well fixing the above code is straight-forward. To make the problem
interesting, you have to fix the above code, by changing exactly one
character. There are 2 known solutions. See if you can get all
those 2.
one solution will be to use n-- instead of i--.
Correct Answer i < n to -i < n
Explanation The correct answer suggests changing the condition in the for loop from "i < n" to "-i < n". This change will ensure that the loop continues until the value of -i becomes greater than n, thus printing the minus sign 20 times as intended.
Rate this question:
6.
Write a “Hello World” program in ‘C’ without using a semicolon.
Correct Answer int main(){if (printf(“Hello World” )){}}
Explanation The given code is a "Hello World" program in C that does not use a semicolon. It uses the printf() function to print the string "Hello World" and the if statement to check if the printf() function returns a non-zero value (which it does in this case). If the condition is true, the code block inside the if statement will be executed. Since there is no other code after the if statement, the program will terminate after printing "Hello World".
Rate this question:
7.
Predict the ouput:
#include
void f()
{
a=10;
printf("%d",a);
}
int a=5;
main()
{
a++,b++;
printf("%d,%d",a,b);
f();
}
Correct Answer compile error
Explanation The code will result in a compile error because the variable "b" is not declared before it is used in the line "a++, b++". Since "b" is not declared, the compiler will not be able to recognize it and will throw a compile error.
Rate this question:
8.
What is wrong with this macro to find out the square of a number
#define square(a) a*a?
Correct Answer ANSWER:
If you pass an expression to this macro, it fails.
Eg: square(2+3) will be expanded to 2+3*2+3 = 11
whereas the actual result should be 25
Inorder to solve this problem you can write the macro as
#define square(a) ((a)*(a))
Explanation The given macro to find the square of a number is incorrect because it does not account for expressions being passed as arguments. When an expression is passed, the macro expands to a faulty calculation, resulting in an incorrect answer. To solve this problem, the macro should be written as #define square(a) ((a)*(a)), which ensures that the expression is properly evaluated before performing the multiplication.
Rate this question:
9.
Will the following program compile successfully?
int main()
{
int a=10;
int &k;
k=a;
}
Correct Answer No. You have to initialize k when it is declared.
int &k=a;
Also you can not assign a const like,
int &k=10;
But after initializing the value of k by writing
int &k=a;
you can assign any integer value to k. This will actually change the value of a.
Explanation The program will not compile successfully because the reference variable "k" is not initialized when it is declared. In order to declare and initialize a reference variable, it must be assigned to a valid memory location. Additionally, a reference variable cannot be assigned a constant value, such as "10". However, after initializing "k" with the value of "a" by writing "int &k=a;", any integer value can be assigned to "k", which will actually change the value of "a".
Rate this question:
10.
Print the output of this program
void main()
{ int a, b, c, abc = 0;
a = b = c = 40;
if (c) {
int abc;
abc = a*b+c;
}
printf ("c = %d, abc = %d ", c, abc);
}
A.
C = 0 and abc = 40
B.
C = 0 and abc = 0
C.
C = 40 and abc = 0
D.
NONE OF THESE
Correct Answer
C. C = 40 and abc = 0
Explanation The program initializes variables a, b, and c to 40. It then checks if the value of c is non-zero (which it is). Inside the if statement, a new variable abc is declared and assigned the value of a*b+c, which is 40*40+40 = 1640. However, this variable abc is local to the if statement and does not affect the value of the outer abc variable. Therefore, when the printf statement is executed, the value of c is still 40, but the value of abc is 0.
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.