C Programming Trivia Quiz

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 Suri143
S
Suri143
Community Contributor
Quizzes Created: 1 | Total Attempts: 2,046
| Attempts: 2,046 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. 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); }

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.

Submit
Please wait...
About This Quiz
C Programming Trivia Quiz - Quiz

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... see morebasic 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! see less

2. Tell the output of this program.

void main()
{
         int cnt = 5, a;
         do {
               a /= cnt;
        } while (cnt --);
        printf ("%d ", a);
}

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.

Submit
3.
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

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

Submit
4. 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--.

Explanation

The correct answer suggests changing the condition in the for loop from "i

Submit
5. Write a "Hello World" program in 'C' without using a semicolon.

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

Submit
6. Predict the ouput: #include void f() {  a=10;  printf("%d",a); } int a=5; main() {  a++,b++;  printf("%d,%d",a,b);  f(); }

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.

Submit
7. What is wrong with this macro to find out the square of a number #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.

Submit
8. Will the following program compile successfully? int main() { int a=10; int &k; k=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".

Submit
9. Tell the output of the given Printf function.
     
  printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));

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

Submit
10. Deallocate memory without using free() in C.

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.

Submit
View My Results

Quiz Review Timeline (Updated): Sep 1, 2023 +

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

  • Current Version
  • Sep 01, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 07, 2013
    Quiz Created by
    Suri143
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Print the output of this program  ...
Tell the output of this program....
Tell the output of this program.#include...
Given is a piece of C code, whose purpose was to print a...
Write a "Hello World" program in 'C' without using a semicolon.
Predict the ouput:...
What is wrong with this macro to find out the square of a number...
Will the following program compile successfully?...
Tell the output of the given Printf function....
Deallocate memory without using free() in C.
Alert!

Advertisement