Languages-C & C Plus Plus [HCL Placement]: Sample Questions 11 - 12 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 11
Question MCQ▾
What is the output of the following program? (. has been used to indicate a space)
main()
{
char s[]="Hello,.World";
printf(%15.10s",s);
}
Choices
Choice (4) | |
---|---|
a. | … . Hello, . Wor |
b. | Hello, . Wor … . |
c. | Hello, . World … |
d. | None of the above |
Answer
a.Explanation
- In the main () function one string variable is defined, s with “Hello, World” .
- The printf statement will print 15 characters of string. Here, given string is shorter than 15. If the given string is smaller than the size, then the “empty” positions will be filled with “white space” . But it will print only 10 characters. Here, white space is added in the beginning of the output.
- So, output will be … . Hello, . Wor
Question 12
Question MCQ▾
Find the output for the following C program
inti =10
main()
{inti =20,n;
for(n=0;n<=i;)
{inti=10;
i++;
}
printf("%d", i);
}
Choices
Choice (4) | |
---|---|
a. | 20 |
b. | 10 |
c. | 12 |
d. | All of the above |
Answer
a.Explanation
- Firstly, one integer i is defined with value 10. In the main () function one more time variable i is defined with 20 and variable n is declared. In for loop n is initialized with 0. In the condition value of n is compared with value of i.
- If it is less than or equal to i then i will be initialized with 10 and gets incremented. After the complete execution of for loop, we want to print the value of i, which is in the main () function. Here, variable i is initialized with 20 so, 20 gets printed on the screen.