Languages-C & C Plus Plus [HCL Placement]: Sample Questions 1 - 2 of 12

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 can be said of the following program?

  1. main()
  2. {
  3.     enumMonths {JAN =1,FEB,MAR,APR};
  4.     MonthsX =JAN;
  5.     if(X==1)
  6.     {
  7.         printf("Jan is the first month");
  8.     }
  9. }

Choices

Choice (4)

a.

Does not print anything

b.

Results in runtime error

c.

Generates compilation error

d.

Prints: Jan is the first month

Answer

d.

Explanation

  • Enumerated in C is a user defined data type. enum keyword is used to define enumerated data type. Here, user defined data type month is created in the first line. There four values are allocated JAN, FEB, MAR, APR.
  • In the next line X variable is declared of type Months which can be initialized with any “data value amongst 4 values” . Default numeric value assigned to first enum value is “0” . Numerically JAN is given value “0” . But here, JAN is initialized with 1, and value of X is also 1. So, in the if block, Jan is the first month will get printed.

Question 2

C & C Plus Plus

Question

MCQ▾

Which is the output produced by the following program

  1. main()
  2. {
  3.     intn=2;
  4.     printf("%d %dn", ++n, nn);
  5. }

Choices

Choice (4)

a.

2,4

b.

3,6

c.

3,4

d.

None of the above

Answer

a.

Explanation

  • In the main () function one integer variable n is defined with value 2. In the printf statement we want to print the value of ++ n and n ⚹ n. We are using % d format specifier it means we want integer values to get printed out on screen.
  • ++ n is pre increment operator of n, where value of n is incremented first and then it is substituted. As current value of n is 2, it will print 3 (incremented by 1) and n ⚹ n means we want to print the multiplication of n and n. As value of n is 2, n ⚹ n will result into 2 ⚹ 2 = 4.
  • So, output of the program will be 2,4.

Developed by: