C Programming Practice - Test 5

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 Nchaudhuri31
N
Nchaudhuri31
Community Contributor
Quizzes Created: 19 | Total Attempts: 9,428
Questions: 20 | Attempts: 121

SettingsSettingsSettings
C Programming Practice - Test 5 - Quiz

. Dear Learner,
This Assessment consists of questions to test your understanding on - C Programming
You may attempt the same and clarify doubts in WP Group. / With your Faculty Member
Regards,
Team SHIVAM


Questions and Answers
  • 1. 

    Point out the error, if any in the for loop #include int main() { int i=1; for(;;) { printf("%d\n", i++); if(i>10) break; } return 0; }

    • A.

      There should be a condition in the for loop

    • B.

      The two semicolons should be dropped

    • C.

      The for loop should be replaced with while loop

    • D.

      No error

    Correct Answer
    D. No error
    Explanation
    The given for loop does not have any errors. It is a valid infinite loop that will continue executing until the condition "i > 10" is met, at which point it will break out of the loop. The code will print the value of "i" and increment it by 1 in each iteration.

    Rate this question:

  • 2. 

    Pick the right alternative main() { int c=- -2; while(--c) printf("c=%d",c); }

    • A.

      C=99

    • B.

      C=1

    • C.

      No Output

    • D.

      C=2

    Correct Answer
    B. C=1
    Explanation
    The code snippet initializes the variable c with the value - -2, which is equivalent to 2. The while loop decrements c by 1 each time it loops until c becomes 0. Since the initial value of c is 2, the loop will execute once and the value of c will become 1. Therefore, the output will be "c=1".

    Rate this question:

  • 3. 

    What will be the output? main() { char s[ ]="m"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

    • A.

      Mmmm

    • B.

      Compiler error

    • C.

      No Output

    • D.

      M01m

    Correct Answer
    A. Mmmm
    Explanation
    The given code defines a character array `s` with the value "m". It then declares an integer variable `i` and initializes it to 0.

    The `for` loop iterates as long as `s[i]` is not a null character. In this case, since `s` only contains one character "m", the loop will execute once.

    Inside the loop, the `printf` statement prints the characters `s[i]`, `*(s+i)`, `*(i+s)`, and `i[s]`.

    In this case, all four characters will be "m" because `s[i]` is equivalent to `*(s+i)` is equivalent to `*(i+s)` is equivalent to `i[s]`, which all refer to the same memory location in the character array `s`.

    Therefore, the output will be "mmmm".

    Rate this question:

  • 4. 

    What main() { int i=0; while(+(+i--)!=0) i-=i++; printf("%d",i); }

    • A.

      0

    • B.

      1

    • C.

      -1

    • D.

      -2

    Correct Answer
    C. -1
    Explanation
    The code snippet starts with initializing the variable i to 0. Then, it enters a while loop where it checks if the value of +(+(i--)) is not equal to 0. The double negative sign before i-- is used to decrement the value of i by 1. However, the unary plus operator before the double negative sign does not have any effect on the value. Inside the loop, i is further decremented by 1 using i-=i++. Finally, the printf statement prints the value of i, which is -1.

    Rate this question:

  • 5. 

    Void main() { static int i; while(i2)?i++:i--; printf(“%d”, i); }

    • A.

      32767

    • B.

      0

    • C.

      1

    • D.

      Compiler error

    Correct Answer
    A. 32767
    Explanation
    The given code snippet initializes the variable 'i' as a static variable. It then enters a while loop where it checks if 'i' is greater than 2. If it is, 'i' is incremented by 1, otherwise, it is decremented by 1. Since the initial value of 'i' is not specified, it can be assumed that it starts at the maximum value for a signed 16-bit integer, which is 32767. Therefore, the code will continue to increment 'i' until it reaches the maximum value, and then it will print the value of 'i', which is 32767.

    Rate this question:

  • 6. 

    What is the output of this program? void main() { int i=122; while(i!=97) i--; while(i>65) i--; printf(“%c”,i); }

    • A.

      65

    • B.

      A

    • C.

      B

    • D.

      B

    Correct Answer
    B. A
    Explanation
    The program starts with the variable i initialized to 122. It then enters a while loop that decrements i until it reaches 97. After that, it enters another while loop that continues to decrement i until it reaches 65. Finally, it prints the character corresponding to the ASCII value of i, which is 65. Therefore, the output of the program is A.

    Rate this question:

  • 7. 

    What is the output of this program? void main() { int i=99; while(i

    • A.

      100

    • B.

      A

    • C.

      D

    • D.

      C

    Correct Answer
    C. D
  • 8. 

    #include void main() { double k = 0; for (k = 0.0; k < 3.0; k++); printf("%lf", k); }

    • A.

      4.000000

    • B.

      3.000000

    • C.

      2.000000

    • D.

      Run time error

    Correct Answer
    B. 3.000000
  • 9. 

    What is the output of this program? void main() { int x=1; x=!x; while(x!=!x) { printf(“%d”,i); } }

    • A.

      0

    • B.

      1

    • C.

      -1

    • D.

      Logical Error

    Correct Answer
    A. 0
    Explanation
    The program initializes the variable x to 1. It then uses the logical negation operator "!" to change the value of x to 0. In the while loop condition, it checks if x is not equal to the logical negation of x. Since the logical negation of x is 1, the condition becomes x != 1, which is false. Therefore, the while loop is not executed and nothing is printed. Hence, the output of the program is 0.

    Rate this question:

  • 10. 

    What is the output of this program? void main() { int k; for(k=10; k%2! =0 && k%3!=0;k--) if(k==1) break; printf(“%d”,k); }

    • A.

      Compiler error

    • B.

      10

    • C.

      7 5

    • D.

      Prime digits

    Correct Answer
    B. 10
    Explanation
    The program starts with initializing the variable k to 10. Then it enters a for loop where it checks two conditions: k%2 != 0 (k is not divisible by 2) and k%3 != 0 (k is not divisible by 3). If both conditions are true, it decrements the value of k by 1.

    In this case, since 10 is divisible by 2, the condition k%2 != 0 is false, and the loop is not executed. Therefore, the value of k remains 10.

    Finally, the program prints the value of k, which is 10.

    Rate this question:

  • 11. 

    What is the output of this program? void main() { long int i=3; while(++i

    • A.

      0

    • B.

      4

    • C.

      3

    • D.

      No output

    Correct Answer
    C. 3
    Explanation
    The output of this program is 3. This is because the variable i is initially set to 3. In the while loop, the expression (++i < 0) is evaluated. Since i is incremented before the comparison, it becomes 4. However, 4 is not less than 0, so the condition is false and the loop is not executed. Therefore, the program does not output anything other than the initial value of i, which is 3.

    Rate this question:

  • 12. 

    What is the output of this program? void main() { int i, j; for(i=0,j=20; i!=j; i++,j--) { printf(“%c”,i+48); } }

    • A.

      Digits

    • B.

      Prime digits

    • C.

      Compiler error

    • D.

      No output

    Correct Answer
    A. Digits
    Explanation
    The program is using a for loop to iterate through the values of i and j. The loop will continue as long as i is not equal to j. Inside the loop, the program is printing the character representation of i+48. Since i starts at 0 and increments by 1 each iteration, the program will print the digits 0 to 9. Therefore, the output of the program will be the digits.

    Rate this question:

  • 13. 

    What is the output of this program? void main() { int i=0, j=1; while(i==!j) { printf(“%d %d”,!i,!j); i++; j++; } }

    • A.

      0 0

    • B.

      0 1

    • C.

      1 0

    • D.

      1 1

    Correct Answer
    C. 1 0
    Explanation
    The output of the program will be "1 0". This is because the while loop condition is checking if i is equal to the logical NOT of j. Initially, i is 0 and j is 1. The logical NOT of j is 0. Since i is not equal to 0, the condition is false and the loop does not execute. Therefore, the printf statement is not executed and there is no output.

    Rate this question:

  • 14. 

    What is the output of this program? void main() { int k; for(k=32768; k

    • A.

      -32768

    • B.

      32768

    • C.

      -32767

    • D.

      32767

    Correct Answer
    A. -32768
    Explanation
    The output of this program is -32768. This is because the for loop starts with k=32768 and continues until k is less than -32768. However, since the initial value of k is already greater than the condition, the loop will not execute and the value of k will remain as -32768.

    Rate this question:

  • 15. 

    The following code stands for what? void main() { int m=60,n=25; if(m!=n) { while(m>n) m-=n; interchange(m,n); // assume the function interchange content of m & n } printf(“%d”,m); }

    • A.

      LCM(m,n)

    • B.

      GCD(m,n)

    • C.

      Max(m,n)

    • D.

      Min(m,n)

    Correct Answer
    B. GCD(m,n)
    Explanation
    The code is checking if m is not equal to n, and if so, it enters a while loop where it subtracts n from m until m becomes less than n. This process is similar to finding the greatest common divisor (GCD) of m and n. Therefore, the code is calculating the GCD of m and n and the correct answer is GCD(m,n).

    Rate this question:

  • 16. 

    Void main() { int a[5] = {1, 2, 3, 4, 5}; int i; for (i = 0; i < 5; i++) if ((char)a[i] == '5') printf("%d\n", a[i]); else printf("FAIL\n"); }

    • A.

      The compiler will flag an error

    • B.

      Program prints 5

    • C.

      Program will compile and print the ASCII value of 5

    • D.

      Program prints FAIL for 5 times

    Correct Answer
    D. Program prints FAIL for 5 times
    Explanation
    The given code will print "FAIL" five times. This is because the code is using the "%d" format specifier in the printf function, which is used for printing integers. However, the elements of the array "a" are of type int, not char. When the code tries to compare the integer value with the character '5', it will not be equal and the "else" block will be executed, printing "FAIL" each time.

    Rate this question:

  • 17. 

    What is the output of this program? #include #include void main() { printf(“%d”, (2*(1+rand()%4)); }

    • A.

      Any even digit

    • B.

      Any odd digit

    • C.

      Any prime number

    • D.

      Any perfect no

    Correct Answer
    A. Any even digit
    Explanation
    The program generates a random number between 1 and 4 using the rand() function. It then multiplies this number by 2 and adds 1 to it. Since the range of the random number is limited to 1-4, the possible outputs of the expression are 3, 5, 7, and 9. All of these numbers are odd digits. Therefore, the correct answer is "any odd digit".

    Rate this question:

  • 18. 

    What is the output of this program? void main() { int i=0,j=1,k; while(i

    • A.

      Prime nos

    • B.

      Perfect nos

    • C.

      Fibonacci nos

    • D.

      Armstrong no

    Correct Answer
    C. Fibonacci nos
    Explanation
    The output of this program will be "Fibonacci nos". This is because the program is incomplete and the while loop condition is not provided. However, based on the given options "Prime nos", "Perfect nos", "Fibonacci nos", and "Armstrong no", it can be inferred that the program is likely checking for and printing Fibonacci numbers.

    Rate this question:

  • 19. 

    Fill in the blank: #include #include void main() { int i,n; scanf(“%d”,&n); for(i=2;isqrt(n)) printf(“%d is ________”,n); }

    • A.

      Armstrong no

    • B.

      Peterson no.

    • C.

      Perfect no

    • D.

      Prime no

    Correct Answer
    D. Prime no
    Explanation
    The given code is missing a condition in the for loop. It should be "i

    Rate this question:

  • 20. 

    What is the output of this program? void main() { int i; for(i=10;--i%3==0;i--) printf(“%d”,i); }

    • A.

      9 6 3

    • B.

      9 6

    • C.

      9

    Correct Answer
    C. 9
    Explanation
    The program starts with the variable i initialized to 10. In the for loop condition, the expression --i%3==0 is evaluated. The --i decrements i by 1 and then checks if the result is divisible by 3. Since 10 is not divisible by 3, the condition is false and the loop exits. Therefore, the program only prints the value of i once, which is 9.

    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
  • Feb 17, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jun 11, 2018
    Quiz Created by
    Nchaudhuri31
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.