Languages-C & C Plus Plus [Redpine Infotech Placement]: Sample Questions 1 - 2 of 5

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 1

C & C Plus Plus

Question

MCQ▾

What is output for the following program?

  1. #include<stdio.h>
  2. main ()
  3. {
  4.     intn=2;
  5.     intsum=5;
  6.     switch (n)
  7.     {
  8.         case2:sum=sum-2;
  9.         case3:sum ⚹=5;
  10.         break;
  11.         default:sum=0;
  12.     }
  13.     printf (“%d” sum);
  14. }

Choices

Choice (4)

a.

6

b.

15

c.

0

d.

None of the above

Answer

b.

Explanation

Table Shows the Program
#include < tdio. h >Standard library function
main ()startup after initialization
int n = 2;

int sum = 5;

Declare integer value n = 2 and sum = 5
switch (n)Switch in n, n = 2, so switch (2)
case 2: sum = sum-2;

case 3: sum ⚹= 5;

In case: 2
  • Sum = sum-2, so, sum = 5 - 2 = 3
  • As there is no break after this case execution continues to next case.

In case: 3

  • Sum ⚹= 5, thus, sum = 3 ⚹ 5 = 15
  • so, sum = 15
break;Switch breaks and sum = 15
default: sum = 0;This statement is not checked
printf ( “% d” sum) ;Print sum = 15

Question 2

C & C Plus Plus

Question

MCQ▾

What is output for the following program?

  1. #include<stdio.h>
  2. main ()
  3. {
  4.     inta=10, b=5;
  5.     if(a=a&b)b=ab;
  6.     printf (“a=%d, b=%d” a, b);
  7. }

Choices

Choice (4)

a.

a = 0, b = 5

b.

a = 10, b = 5

c.

a = 0, b = 0

d.

None of the above

Answer

a.

Explanation

Table Shows the Program
#include < tdio. h >Standard library function
main ()Startup after initialization
int a = 10, b = 5;Declare integer values a = 10 and b = 5
if (a = a&b) b = ;In if condition (a = a&b) b = , when given a = 10 (00001010) , b = 5

(00000101)

(a = a&b) = 00000000. This is also false thus a = 0 and b = 5 (already given)

printf ( “a =% d, b =% d” a, b) ;Print a = 0 and b = 5

Developed by: