0

C Expressions

Description: Concept Based Questions
Number of Questions: 15
Created by:
Tags: Concept Based Questions Programming - C / C++
Attempted 0/15 Correct 0 Score 0

What is the output of the given program?

int main()
{
printf(%d,128>>4>>3>>2);
// Line 1
}

  1. 2

  2. 4

  3. 0

  4. 8

  5. Error at line 1


Correct Option: C
Explanation:

This is the correct answer. The operator ( >>) is a right shift operator. The general formula to solve (N >> s) is: N/2^s. Here, 128>>4 will be solved first which is 128/(2^4) = 8. So, the statement now becomes 8>>3>>2. Now 8/(2^3) will be solved which will return (8/8) = 1. Now again the statement becomes 1>>2. So, 1/(2^2) will be solved resulting in 0. The correct output is zero.

What is the output of the given program?

int main()
{
 printf(%d,128<<4<<3<<2>>6);
// Line 1
}

  1. 65536

  2. 1024

  3. 0

  4. 512

  5. Error at line 1


Correct Option: B
Explanation:

Two kinds of operators are used in the statement: left shift (<<) and right shift (>>). The general formula to solve (N << s)  is: N * 2^s, and the general formula to solve (N >> s) is: N/2^s. So, firstly 128<<4<<3<<2 will be solved which will return 128 * (2^4) * (2^3) * (2^2) = 65536. Now the statement becomes 65536 >> 6. So, it will return 65536/2^6 = 1024. The output will be 1024. So, this is the correct answer.

What is the output of the given program?

int main()
 {
 int x = 24;
 int y = 42;
 printf("%d",(~x) | (~y) );
 }

    • 25
    • 43
    • 9
    • 6
  1. Compiler error


Correct Option: C
Explanation:

This is the correct answer. The compiler will first reverse the bits of  x and y as we have used ~ (tilde) operator on them. So, the expression becomes: -24 | -43. Now, the compiler will convert -25 and -43 into binary and perform OR operation on them. So, the answer will be -9.

What is the output of the given program?

int main()
{
 int x = 24;
 int y = 42;
 int z = 37;
 int a = 22;
 printf(%d,x&y|z^a);
 }

  1. 60

  2. 59

  3. 58

  4. 57

  5. 56


Correct Option: B
Explanation:

This is the correct answer. The operators used here are all bitwise operators which operate on binary bits. So, the value of x in binary is 00011000, the value of  Y in binary is 00101010 and the value of Z in binary is 00100101. The value of variable "a" in binary is 00010110. The expression becomes [ 000011000 & 00101010 | 00100101 ^ 00010110 ]. So, we will evaluate 24 and 42 | 37 ^ 22, which results in 59.

What is the output of the given code?

int main()
{
 int x = 24; int y = 42;
 int z = 37; int a = 22;
 printf(%d,x|y|z|a );
 }

  1. 64

  2. 59

  3. 62

  4. 63

  5. 77


Correct Option: D
Explanation:

The compiler will first convert all of the given values in binary form. So, value of x in binary is 00011000, value of Y in binary is 00101010, value of Z in binary is 00100101 and value of "a" in binary is 00010110. Now, OR operation will be performed on all of them to obtain a single result: (00011000) | (00101010) | (00100101) | (00010110). It will result in 63. So, this is the correct answer.

What is the output of the given code?

int main()
 {
 printf("%d\\n",pow(128>>3>>2>>1,3));
 }

  1. 16

  2. 8

  3. 0

  4. 4

  5. Compiler error


Correct Option: C
Explanation:

This is the correct answer because we have used %d access specifier in printf() function that is used to print the integer values, but pow() function returns a double value. So, we have to use %lf to print the original value. Zero will get printed on the screen.

What is the output of the given code?

int main()
 {
 int x = 24; int y = 42;
 int z = 37; int a = 22;
 printf("%d",x&y^z|x~a );
 }

  1. 63

    • 63
  2. 74

    • 74
  3. Compiler error


Correct Option: E
Explanation:

This is the correct answer because ~ (tilde) operator is unary operator. So, it can be applied only on a single operand. But here we are using it on two operands (x~a). So, it will generate an error.

What is the output of the given code?

int main()
 {
 switch(printf("%d\\n",pow(128>>3>>2>>1,3))); case 56/4 :
printf("Hello"); case 32/4 : Printf("Bye"); case 0/9 :
Printf("Good"); case 0/1 : Printf("Bad");
 }

  1. HelloByeGoodBad

  2. ByeGoodBad

  3. GoodBad

  4. Bad

  5. Compiler error


Correct Option: E
Explanation:

There is an error in the code because we have put a semicolon after the switch() statement which is invalid. The compiler will raise an error “case label not with a switch statement”. So, this answer is correct.

What is the output of the given code?

int main()
 {
 switch(printf("%d\\n",pow(128>>3>>2>>1,3)))
 {
 case 56/4 : printf("Hello"); case 32/4 :
printf("Bye"); case 0/9 :
printf("Good"); case 0/1 :
printf("Bad");
 }
 }

  1. 0 GoodBad

  2. 8ByeGoodBad

  3. 14HelloByeGoodBad

  4. 16

  5. Compiler error


Correct Option: E
Explanation:

This is the correct choice because the last two cases: “case 0/9” and “case 0/1” both will evaluate to 0. But we cannot have two duplicate case values, so the compiler will raise an error.

What is the output of the given code?

int main()
 {
 int i=2;
 int j=3;
 int k=127;
 printf("%h",++k>>++j<

  1. 32

  2. 20

  3. 16

  4. Blank Output Screen

  5. Compiler error


Correct Option: D
Explanation:

This is the correct choice because the correct answer is 20, but it will not be printed on the screen because we have used specifier %h in the printf() function which is not recognised by the compiler. To successfully print the output in hexadecimal form on the screen we must use %hx. So, blank output will be there on the screen.

Find out the output of the given program, if any, or find out the error. Give the answer of the program in binary language.

int main()
{
int a=7,b=15,c;
c=a|b;
printf("the result comes out to be is :- %d",c);
}

  1. The result comes out to be: 0111.

  2. The result comes out to be: 1111.

  3. The result comes out to be is: 1000.

  4. The result comes out to be is: 0110.

  5. Compiler error


Correct Option: B
Explanation:

This program will just OR the binary values of 7 and 15. So, after ORing them, the result comes out to be 15 in binary which is 1111. So, this option is correct.

What is the output of the given code?

int main()
{
 int i=2;
 int j=3;
 int k=127;
 printf(%hx,++k>>++j<

  1. 32

  2. 20

  3. 16

  4. 4

  5. Compiler error


Correct Option: B
Explanation:

This is the correct choice. First the value of variable k will be incremented and it will become 128 as we have used pre-increment operator and also the value of variable j will be pre-incremented and will become 4. So, the expression becomes 128>>4>>2. Variable i will be incremented after the evaluation of expression because we have used post-increment operator with it. So, solving this expression, the result comes out to be: 128/(2^4)/(2^2) => 32. Now as we have used %hx in the printf statement, so the value of 32 will be converted to hexadecimal, which is 20. So, 20 is the correct answer.

What is the output of the given code?

int main()
{
 int i = 128<<2<<1<<0;
 switch(i)
 {
 default : printf(Bye); case 128<<4<<3<<2>>6 :
 printf(\\\\nHello); case 128<<4<<2<<2>>6 :
 printf(\\\\nKeep Calm); case 128<<4<<4<<2>>3 :
 printf(\\\\nGet Lost);
 }
 }

  1. ByeHello Keep CalmGet Lost

  2. Hello Keep CalmGet Lost

  3. Keep CalmGet Lost

  4. Get Lost

  5. Compiler error


Correct Option: B
Explanation:

The value of variable [ i ] after evaluating the expression would be (128 * (2^2) * (2^1) * (2^0)) = 1024. So, the switch case would become switch (1024). Now, the case expressions will be solved and the case having value 1024 will be executed. The first case evaluates to 1024. Therefore, all the statements of first case and all the other preceding cases will be executed until a break keyword is found. So, the output will be: Hello Keep CalmGet Lost. So, this answer is true.

What is the output of the given program?

int main()
 {
 int i = 128>>3>>2>>2; switch(i)
 {
 printf("Bye"); case 128<<4<<3<<2>>6 :
 printf("\\nHello"); case 128<<4<<2<<2>>6 :
 printf("\\nKeep Calm"); case 128>>3>>2>>9 :
 printf("\\nGet Lost");
 }
 }

  1. ByeHelloKeep CalmGet Lost

  2. HelloKeep CalmGet Lost

  3. Keep CalmGet Lost

  4. Get Lost

  5. Blank screen


Correct Option: E
Explanation:

The value of variable 'i', after evaluating the expression, would be (128 / (2^3) / (2^2) / (2^2)) = 1. So, the switch case would become switch(1). Now the case expressions will be solved and the case having value 1 will be executed. None of the cases has value 1; so no output will be printed on the screen. So, this answer is true.

What is the output of the given code?

int main()
{
 int i = 128<<4<<3<<2>>6;
 switch(i) { default : printf(Bye); case 65536 :
printf(\\\\nHello); case 32 :
printf(\\\\nGood Bye); case 8/2 :
printf(\\\\nSee U Later);//Line 1
 }
 }

  1. Hello Good ByeSee U Later

  2. Bye

  3. Bye Hello Good Bye See U Later

  4. Good ByeSee U Later

  5. Error at line 1


Correct Option: C
Explanation:

This is the correct answer. The value of variable [ i ], after evaluating the expression (128<<4<<3<<2>>6), will be 1024. The expression will be evaluated as: (128 * (2^4) * (2^3) * (2^2)) / (2^6) = 65536/64 = 1024. But we do not have any case of 1024 in switch statement; so default case will be executed and it will print “Bye”. But after default, we don’t have any break keyword; so all the cases will be executed until the break keyword is found. So, the correct output will be: Bye Hello Good Bye See U Later.

- Hide questions