Quiz - 2 Codegeeks

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 Vivek Rajpoot
V
Vivek Rajpoot
Community Contributor
Quizzes Created: 1 | Total Attempts: 97
Questions: 16 | Attempts: 97

SettingsSettingsSettings
Computer Science Quizzes & Trivia

Questions and Answers
  • 1. 

    What is output: printf("%d" printf("hello") );

    • A.

      Hello

    • B.

      Hello0

    • C.

      Hello5

    • D.

      None

    Correct Answer
    C. Hello5
    Explanation
    The printf function is used to print the output. In this code, the inner printf function "printf("hello")" will print "hello" on the console. The outer printf function will then print the number of characters printed by the inner printf function, which is 5. Therefore, the output will be "hello5".

    Rate this question:

  • 2. 

    What is output: printf( "%d", printf('' %d" , printf("hii"));

    • A.

      Hi31

    • B.

      Hii31

    • C.

      Hii

    • D.

      None

    Correct Answer
    B. Hii31
    Explanation
    The code snippet uses nested printf statements. The innermost printf("hii") statement prints "hii" and returns the number of characters printed, which is 3. The next printf statement ("%d", 3) prints the value 3. Finally, the outermost printf statement ("%d", 3) prints the value 3 as well. So the overall output is "hii31".

    Rate this question:

  • 3. 

    What is output: int a = 10, b = 20, c = 30; printf("%d %d %d");

    • A.

      102030

    • B.

      30 20 10

    • C.

      10

    • D.

      Any value between 10 , 20 , 30

    Correct Answer
    B. 30 20 10
    Explanation
    The correct answer is "30 20 10". The printf function is used to print the values of variables a, b, and c. The format specifier "%d" is used to print the values as integers. Since the variables are printed in the order a, b, c, the output will be 30 20 10.

    Rate this question:

  • 4. 

    What is the output: printf(" %d " , printf("%dhii", printf("hello")));

    • A.

      Hellohi53

    • B.

      Hello5hii4

    • C.

      Hellohi

    • D.

      Hello5hii1

    Correct Answer
    B. Hello5hii4
    Explanation
    The output is "hello5hii4" because the printf function is being nested within itself. The innermost printf statement "printf("hello")" prints "hello" and returns the number of characters printed, which is 5. Then, the next printf statement "printf("%dhii", 5)" prints "5hii" and returns the number of characters printed, which is 4. Finally, the outermost printf statement "printf(" %d ", 4)" prints the value of 4 and returns the number of characters printed, which is also 4. Therefore, the overall output is "hello5hii4".

    Rate this question:

  • 5. 

    In which line there is an error in the program : 1 int main() 2 { 3    char ch[10]; 4   scanf("%s", ch); 5    printf("%s", ch); 6    return 0; 7 }

    • A.

      1

    • B.

      2

    • C.

      4

    • D.

      5

    Correct Answer
    C. 4
    Explanation
    The error in the program is in line 4. The format specifier used in the scanf function is "%s" which is used for reading a string. However, the variable "ch" is declared as a character array with a size of 10, which means it can only store a string of maximum length 9 (including the null character). This can lead to a buffer overflow if the user enters a string longer than 9 characters. To fix this error, the size of the character array should be increased or a limit should be imposed on the input string length.

    Rate this question:

  • 6. 

    What is the value of num? int num = 4/3*2.4;

    • A.

      3

    • B.

      3.33

    • C.

      2

    • D.

      2.0

    Correct Answer
    C. 2
    Explanation
    The value of num is 2 because the expression 4/3*2.4 is evaluated from left to right according to the order of operations. First, 4 divided by 3 equals 1.3333. Then, 1.3333 multiplied by 2.4 equals 3.2. However, since the variable num is declared as an int, the decimal portion is truncated, resulting in the value 3. Therefore, the correct answer is 2.

    Rate this question:

  • 7. 

    What is the value of the num: int num = -17 % 4;

    • A.

      1

    • B.

      4

    • C.

      -1

    • D.

      -4

    Correct Answer
    C. -1
    Explanation
    The value of the variable "num" is -1. The expression "-17 % 4" calculates the remainder when -17 is divided by 4. Since -17 divided by 4 is -4 with a remainder of -1, the value of "num" is -1.

    Rate this question:

  • 8. 

    What is the value of the num: int num = 17 % -4;

    • A.

      -1

    • B.

      1

    • C.

      4

    • D.

      -4

    Correct Answer
    B. 1
    Explanation
    The expression "17 % -4" calculates the remainder when 17 is divided by -4. In this case, the result is 1 because when 17 is divided by -4, the quotient is -4 and the remainder is 1. Therefore, the value of the variable "num" is 1.

    Rate this question:

  • 9. 

    What is the output of the following program? int num = 10; while ( num < 12 ) {     cout << num;     num++; }

    • A.

      101112

    • B.

      1011

    • C.

      1112

    • D.

      None

    Correct Answer
    B. 1011
    Explanation
    The program starts with the variable "num" initialized to 10. The while loop condition checks if "num" is less than 12. Since 10 is less than 12, the loop is executed. Inside the loop, the value of "num" is printed, which is 10. Then, "num" is incremented by 1. The loop continues until "num" reaches 12. Therefore, the output of the program is "1011".

    Rate this question:

  • 10. 

    What is the output : int num = 10; while(num < 12 ); {      cout << num ;      num++; }

    • A.

      Error

    • B.

      Nothing is printed on console

    • C.

      1011

    • D.

      2111

    Correct Answer
    B. Nothing is printed on console
    Explanation
    The while loop condition is not properly defined. The semicolon after the while statement creates an empty loop body, which means the loop will keep executing indefinitely without any code to execute. As a result, nothing will be printed on the console.

    Rate this question:

  • 11. 

    What is the output: int num = 10; while( num < 10 ) {      cout << num;      num++; }

    • A.

      Error

    • B.

      1011

    • C.

      109

    • D.

      Nothing is printed on the screen

    Correct Answer
    D. Nothing is printed on the screen
    Explanation
    The condition in the while loop is "num < 10", but the initial value of "num" is already 10. Since the condition is false from the start, the code inside the loop will not be executed at all. Therefore, nothing will be printed on the screen.

    Rate this question:

  • 12. 

    What is output  #include<stdio.h> int main() {       int a = 500, b, c;       if(a>=400)            b = 300;       c = 200;       printf("%d %d", b, c); }

    • A.

      Grbage_value 200

    • B.

      300 200

    • C.

      200 300

    • D.

      300 grbage_value

    Correct Answer
    B. 300 200
    Explanation
    The correct answer is 300 200. In this code, the variable "a" is assigned a value of 500. The if statement checks if "a" is greater than or equal to 400, which is true. Therefore, the value of "b" is assigned 300. The value of "c" is assigned 200. Finally, the printf statement prints the values of "b" and "c", which are 300 and 200 respectively.

    Rate this question:

  • 13. 

    What is output of following program: #include< stdio.h> int main() {      int i = 65;      char j = 'A';      if( i == j )            printf("C is WOW \n");      else              printf(" C is headache \n");        return 0;    }

    • A.

      C is headache

    • B.

      C is WWO

    • C.

      C is WOW

    • D.

      Nothing

    Correct Answer
    C. C is WOW
    Explanation
    The program compares the integer value of variable i (65) with the ASCII value of character j ('A'), which is also 65. Since the condition i == j is true, the program executes the code inside the if statement and prints "C is WOW" as the output.

    Rate this question:

  • 14. 

    IN which line there is an error  1 #include <stdio.h> 2 int main() 3 { 4     int x = 10, y = 15;      (x + 1) = 15; 5     if( x%2 == y%3) 6             printf("Carpathians\n"); 7     8 }

    • A.

      4

    • B.

      5

    • C.

      2

    • D.

      7

    Correct Answer
    A. 4
  • 15. 

    What will be the output of the following program? #include int main() {        int i = 4, j = -1, k = 0, w,  x, y, z;        w = i || j || k;        x = i && j && k;        y = i || j && k;         z = i && j || k;        printf(" w = %d x = %d y = %d z = %d \n" , w, x, y,z);       return 0; }

    • A.

      W = 0 x = 1 y == 1 z =1

    • B.

      W = 1 x = 0 y == 1 z =1

    • C.

      W = 0 x = 1 y == 1 z =0

    • D.

      W = 0 x = 1 y == 0 z =1

    Correct Answer
    B. W = 1 x = 0 y == 1 z =1
    Explanation
    The program is using logical operators to assign values to variables w, x, y, and z. The logical OR operator (||) returns true (1) if at least one of the operands is true. In the case of w = i || j || k, since i is true (non-zero), the expression evaluates to true and w is assigned the value 1.

    The logical AND operator (&&) returns true (1) only if all of the operands are true. In the case of x = i && j && k, since j is false (0), the expression evaluates to false and x is assigned the value 0.

    The expression y = i || j && k is evaluated from left to right. Since i is true (non-zero), the expression i || j evaluates to true. Then, the expression true && k evaluates to the value of k. Since k is false (0), the expression evaluates to false and y is assigned the value 0.

    The expression z = i && j || k is evaluated from left to right. Since i is true (non-zero), the expression i && j evaluates to the value of j. Since j is false (0), the expression evaluates to false. Then, the expression false || k evaluates to the value of k. Since k is true (non-zero), the expression evaluates to true and z is assigned the value 1.

    Rate this question:

  • 16. 

    What is the output: #include< stdio.h > int main() {      int n =9;      ( (n == 9.5) ? printf(" Correct \n ") : printf(" wrong \n") );       return 0; }

    • A.

      Correct

    • B.

      Wrong

    • C.

      Correct wrong

    • D.

      None

    Correct Answer
    B. Wrong
    Explanation
    The output of the code is "wrong". This is because the expression (n == 9.5) evaluates to false since n is an integer and 9.5 is a floating-point number. Therefore, the second part of the ternary operator is executed, which is to print "wrong".

    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
  • Mar 30, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 04, 2018
    Quiz Created by
    Vivek Rajpoot
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.