Programming Questions & Answers -
You should practice these quizzes to improve your C programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams.
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;
#include <stdio.h> int main() { printf("Hello World! %d \n", x); return 0; }
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!
#include <stdio.h> int main() { int main = 3; printf("%d", main); return 0; }
a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
#include <stdio.h> int main() { char chr; chr = 128; printf("%d\n", chr); return 0; }
a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned
#include <stdio.h> int main() { char *p[1] = {"hello"}; printf("%s", (p)[0]); return 0; }
a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned
#include <stdio.h> int main() { printf("crazyfor\code\n"); return 0; }
a) crazyforcode
b) crazyfor
code
c) codeyfor
d) crazyfor
a) !=
b) ==
c) ||
d) =
#include <stdio.h> int main() { int a = 10; if (a == a--) printf("TRUE 1\t"); a = 10; if (a == --a) printf("TRUE 2\t"); }
a) TRUE 1
b) TRUE 2
c) TRUE 1 TRUE 2
d) No output
a) Within the block it appears
b) Within the blocks of the block it appears
c) Until the end of program
d) Both (a) and (b)
a) true
b) false
c) Depends on the standard
d) None of the mentioned
Can anybody please explain me why is the answer for q#8
#include
int main()
{
int a = 10;
if (a == a–)
printf(“TRUE 1\t”);
a = 10;
if (a == –a)
printf(“TRUE 2\t”);
}
is TRUE1 TRUE2
Its True 2.
the answer shold be TRUE 1
there is a term called “sequence point” in c programming. Until the squence point is not over in the expression, the side effect of the operation remains in the expression and standard c doesn’t guarantee the output.
Here, the statement
if(a == a–) there is a side effect and compiler doesn’t guarantee its result.
similar for if(a == –a).
In gcc compiler the output is TRUE2 which is not matching with the output given in the solution of the question.
why
See TRUE1 should be clear to you. And TRUE2 is also true because –a is evaluated before the whole expression so a becomes 9 before comparison starts hence on both side its 9 == 9 that is true.
thanks a lot! now i am clear about the question and its answer.
But after 1st statement they have again declare a=10 then how the value become 9
step1:a=10;
step2: a–(a=a-1=>a=9);
step3:9==9;
STEP4;display TRUE 1
step1:a=10;
step2: a–(a=a-1=>a=9);
step3:9==9;
STEP4;display TRUE 2
OUTPUT IS TRUE1 TRUE 2
its a correct ,,,,TRUE2 is the correct answer
Operator precedence
#6 is wrong, there is no \c escape sequence…
To,
surendra maharajan
1st let me tell u that — before variable is knowm as prefix operator and other one is postfix operator,
in prefix operator first the value of the variable is changed and then its assigned to the left variable,
in case of if(a==a–),
so the value of a is defined to be 10,and when a– is executed it first decreases the value of a by 1 below(==) operator and now the value of a is 9 and now the work of (a–) comes thats it has to decrease by 1,since the value of a is now 9 the value will become 8 for (a–),
now what happens in
if(a=–a)
value of a=10;
first it will become 9 due to(–a) and then it will assign to 9 to the left variable,so the value of a is 9 on both side,
thank u
for #8 given answer is correct, a becomes 9 when –a and comparison results if(9==9) ok, what happens if (–a==a) in this case also TRUE2 is coming
answer is option c.true 1 true 2!
! because( – - ) decrement operator has more preority than (==) equal too !! so in both if condition before comparing the decrement is done hence option C is the correct amswer
THANK YOU
int main()
{
int x = 1;
short int i = 2;
float f = 3.5;
if(sizeof((x == 2) ? f : i) == sizeof(float))
printf(“float”);
else
printf(“int”);
}
#4
Depends on the compiler when you run this code on GCC compiler you will get 128 as output.
#3
It will run without error and prints 3
#8
Answer is True 2.
#9
Answer is a
question no 6 i don’t think so which you mentioned is correct answer, because i got output as carzyforcode
it think 6th one answer is crazyforcode. if the printf statement is printf(“crazyfo\rcode\n”); then the output will be codeyfo.
True 1 correct because a– is become 10 it is post decremeny