Sail E0 Webinar

MCQs

Total Questions : 35 | Page 3 of 4 pages
Question 21.


What will be the output of the program?


#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i || ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
  1.    2, 2, 0, 1
  2.    1, 2, 1, 0
  3.    -2, 2, 0, 0
  4.    -2, 2, 0, 1
 Discuss Question
Answer: Option D. -> -2, 2, 0, 1

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i || ++j && ++k; here (++j && ++k;) this code will not get executed because ++i has non-zero value.
becomes m = -2 || ++j && ++k;
becomes m = TRUE || ++j && ++k; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d`setminus`n", i, j, k, m); In the previous step the value of variable 'i' only increemented by '1'(one). The variable j,k are not increemented.

Hence the output is "-2, 2, 0, 1".



Question 22.


Assuming, integer is 2 byte, What will be the output of the program?


#include<stdio.h>
int main()
{
printf("%x\n", -2
  1.    ffff
  2.    0
  3.    fff8
  4.    Error
 Discuss Question
Answer: Option C. -> fff8

The integer value 2 is represented as 00000000 00000010 in binary system. 
Negative numbers are represented in 2's complement method. 
1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0). 
2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value). 
Therefore, in binary we represent -2 as: 11111111 11111110. 
After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system.


Question 23.


What will be the output of the program?


#include<stdio.h>
int main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return 0;
}
  1.    -2, 3, 1, 1
  2.    2, 3, 1, 2
  3.    1, 2, 3, 1
  4.    3, 3, 1, 2
 Discuss Question
Answer: Option A. -> -2, 3, 1, 1

Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.

Step 2: m = ++i && ++j && ++k;
becomes m = -2 && 3 && 1;
becomes m = TRUE && TRUE; Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.

Step 3: printf("%d, %d, %d, %d`setminus`n", i, j, k, m); In the previous step the value of i,j,k are increemented by '1'(one).

Hence the output is "-2, 3, 1, 1".



Question 24.

In which order do the following gets evaluated

1.Relational

2.Arithmetic

3.Logical

4.Assignment


  1.    2134
  2.    1234
  3.    4321
  4.    3214
 Discuss Question
Answer: Option A. -> 2134

2. Arithmetic operators: *, /, %, +, - 
1. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators : !, &&, ||
4. Assignment operators: =


Question 25.

Which of the following are unary operators in C?

1.!

2.sizeof

3.~

4.&&


  1.    1, 2
  2.    1, 3
  3.    2, 4
  4.    1, 2, 3
 Discuss Question
Answer: Option D. -> 1, 2, 3

An operation with only one operand is called unary operation.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator.

&& Logical AND is a logical operator.

Therefore, 1, 2, 3 are unary operators.


Question 26.

Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();


  1.    f1, f2, f3
  2.    f3, f2, f1
  3.    Order may vary from compiler to compiler
  4.    None of above
 Discuss Question
Answer: Option C. -> Order may vary from compiler to compiler

Here, Multiplication will happen before the addition, but in which order the functions would 

be called is undefined. In an arithmetic expression the parenthesis tell the compiler which 

operands go with which operators but do not force the compiler to evaluate everything within

 the parenthesis first.


Question 27.

Which of the following is the correct usage of conditional operators used in C?


  1.    a>b ? c=30 : c=40;
  2.    a>b ? c=30;
  3.    max = a>b ? a>c?a:c:b>c?b:c
  4.    return (a>b)?(a:b)
 Discuss Question
Answer: Option C. -> max = a>b ? a>c?a:c:b>c?b:c

Option A: assignment statements are always return in paranthesis in the case of conditional 

operator. It should be a>b? (c=30):(c=40);

Option B: it is syntatically wrong.

Option D: syntatically wrong, it should be return(a>b ? a:b);

Option C: it uses nested conditional operator, this is logic for finding greatest number out of 

three numbers.


Question 28.

Which of the following correctly shows the hierarchy of arithmetic operations in C?


  1.    / + * -
  2.    * - / +
  3.    + - / *
  4.    / * + -
 Discuss Question
Answer: Option D. -> / * + -

Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).

How Do I Remember ? BODMAS !

  • B - Brackets first
  • O - Orders (ie Powers and Square Roots, etc.)
  • DM - Division and Multiplication (left-to-right)
  • AS - Addition and Subtraction (left-to-right)

  • Question 29.

    In the following code in which order the functions would be called?


    a=f1(23,14)*f2(12/4)+f3()


    1.    f1,f2,f3
    2.    f3,f2,f1
    3.    The order may vary from compiler to compiler
    4.    None of the above
     Discuss Question
    Answer: Option C. -> The order may vary from compiler to compiler

    Here the multiplication will happen before the addition, but in which order the functions would be called is undefined



    Question 30.

    Which of the following is the correct order of evaluation for the below expression?
    z = x + y * z / 4 % 2 - 1


    1.    * / % + - =
    2.    = * / % + -
    3.    / * % - + =
    4.    * % / - + =
     Discuss Question
    Answer: Option A. -> * / % + - =

    C uses left associativity for evaluating expressions to break a tie between two operators

     having same precedence.


    Latest Videos

    Latest Test Papers