Tricky C/C++/JAVA/.NET Questions
| 1)[C] Which is efficient?
(a) or (b)
a) main(){ int i,j; for(i=0;i<=100;i++) for(j=0;j<=500;j++) printf("%d %d",i,j); } |
b) main(){
int i,j; for(j=0;j<=500;j++) for(i=0;i<=100;i++) printf("%d %d",i,j); } |
| Ans: the light-weighted inner loop is efficient,generally. Since the second snippet has the less inner loop iterations less than the outside loop, it is more efficient than the first one. | |
| 2)[C]What's the
output?
#define sqr(x) (x * x) main() { int i=2,r; r=sqr(i+3); printf("%d",r); } | |
| Ans: 11. Surprising? 'i' in the sqr(i+3) is replaced with 2 and this macro is expanded as 2+3 * 2+3.But not 5 * 5. As the * has the highest priority, it becomes 2+6+3 = 11. Gopalakrishnan.P, Thiagaraja Engineering College, Madurai | |
| 3. [C++]
#include <iostream.h>
main() { int i=0; if(i = = 0) cout<<"i="<<i; } | |
| Ans.i=0. Because the (i = = 0) is true and thus it becomes if(1). It leads i=0 to get printed. Karthikeyan | |
| 4. [C++]
#include <iostream.h>
main() { int i=0; if(i = 0) cout<<"i="<<i; } | |
| Ans. No output.= is the assignment operator. In if(i=0), the value 0 is assigned to i and it becomes if(0). Now it searches for an 'else' part. Since it was not there, there would be no output. Pazhamalai Nathan.G | |
| 5.[C]
#include | |
| Ans.a = xxx *
10 which is => a = ABC - XYZ * 10 => a = 20 - 10 * 10 => a = 20 - 100 => a = -80. Rebecca David, Final-CSE,Easwari Engineering College. | |
| 6.[C]
#include | |
| Ans. the
result will be c = 40 and abc = 0; because the scope of the variable 'abc' inside if(c) {.. } is not valid out side that if (.) { .. }. Rebecca David, Final-CSE,Easwari Engineering College. | |
| 7. [C] #include <stdio.h> main(){ if(printf("")) printf("True"); else printf("False"); } | |
| Ans: printf() function will return an integer that is number of characters printed by that printf function. Here inside the condition no character is printed ( "" ). so, the condition becomes false. The output will be "False" | |
|
You can add some more tricky questions and its answers through email.Subject of the email with the prefix:(TQS). Email-id: [email protected] |