0% found this document useful (0 votes)
509 views8 pages

C Code Output Analysis and Errors

The document contains 15 multiple choice questions related to C programming concepts such as operators, data types, control flow statements, functions etc. The questions test understanding of precedence rules, side effects, return values and output of sample code snippets.

Uploaded by

vasanthi2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
509 views8 pages

C Code Output Analysis and Errors

The document contains 15 multiple choice questions related to C programming concepts such as operators, data types, control flow statements, functions etc. The questions test understanding of precedence rules, side effects, return values and output of sample code snippets.

Uploaded by

vasanthi2014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is the difference between the following 2 codes?

#include <stdio.h> //Program 1


int main()
{
int d, a = 1, b = 2;
d = a++ + ++b;
printf("%d %d %d", d, a, b);
}
#include <stdio.h> //Program 2
int main()
{
int d, a = 1, b = 2;
d = a++ +++b;
printf("%d %d %d", d, a, b);
}

a) No difference as space doesnt make any difference, values of a, b, d are


same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not

2. What is the output of this C code?


` #include <stdio.h>
int main()
{
int a = 1, b = 1, c;
c = a++ + b;
printf("%d, %d", a, b);
}
a) a = 1, b = 1
b) a = 2, b = 1
c) a = 1, b = 2
d) a = 2, b = 2

3. What is the output of this C code?


#include <stdio.h>
int main()
{
int a = 10, b = 10;
if (a = 5)
b--;
printf("%d, %d", a, b--);
}
a) a = 10, b = 9
b) a = 10, b = 8
c) a = 5, b = 9
d) a = 5, b = 8

4. What is the output of the C statement 7.5 % 3?


a) 1 b) 1.5 c) error d) no output

5. What is the output of this C code?


void main()
{
int a = 500, b = 100,c;
if(!a>=400)
b = 300;
c = 200;
printf ("b = %d c = %d",b,c);
}

(a) 100, 200 (b) 300, 200 (c) 100, 0 (d) 300, 0

6. What is the output of this C code?


void main()
{
int c = 0, d = 5, e = 10, a;
a = c> 1 ? d > 1 || e > 1 ? [Link];
printf ("a = %d", a);
}

(a) 100 (b) 200 (c) 300 (d) None of the above

7. What is the output of this C code?


void main()
{
printf ("bytes occupied by 7 = %d", sizeof ( 7 ) ) ;
printf ("bytes occupied by 7 =%d", sizeof ( 7 ) ) ;
printf ("bytes occupied by 7.0 = %d", sizeof (7.0));
}

(a) 1 2 4 (b) 1 2 8 (c) 2 2 4 (d) 2 2 8


8. What is the output of this C code?
void main()
{
printf ("%d %f \n", 4,4);
printf ("%d%f\n", 4.0,4.0);
}

(a) 4 0.000000
0 4.000000
(b) 4 4.000000
0 4.000000
(c) 4 4.000000
4 4.000000
(d)4 0.000000
4 4.000000

9. What is the output of this C code?


void main()
{
int i = 3, a = 4, n;
float t = 4.2;
n = a*a/i + i/2*t + 2 + t;
printf ("n = %d", n ) ;
}

(a) 16 (b) 15 (c) 14 (d) 17

10. Which bitwise operator is suitable for checking whether a particular bit is
on or off?
A B
&& operator & operator
. .
C D
|| operator ! operator
. .

11. What is the output of this C code?


#include<stdio.h>
int main()
{
int k;
k = (1,2,3,4,5);
printf("%d\n", k);
return 0;
}
a) 2 b) 5 c) 1 d) 4

12. What is the output of this C code?


#include<stdio.h>
int main()
{
int k, num = 100;
k = (num > 50 ? (num <= 10 ? 100 : 200): 500);
printf("%d\n", num);
return 0;
}

a) 500 b) 200 c) 100 d) 300

13. What is the output of this C code?


#include<stdio.h>
int main()
{
int x=10;
printf("%d, %d, %d\n", x <= 10, x = 40, x >= 10);
return 0;
}

a) 1, 40, 1 b) 0, 1, 1 c) 1, 1, 1 d) 0, 40, 1

14. How many times "IndiaBIX" is get printed?


#include<stdio.h>
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
printf("IndiaBIX");
}
return 0;
}

a B
Infinite times 11 times
. .
C D
0 times 10 times
. .

15. Which of the following cannot be checked in a switch-case statement?


A B
Character Integer
. .
C D
Float enum
. .

16. What is the size of an int data type?


a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined

17. Which of the following statement is used to take the control to the
beginning of the loop?
a) break b) continue c)exit d) none of these

18. What will be the output of the program?


#include<stdio.h>
int main()
{
int i=0;
for(; i<=5; i++);
printf("%d", i);
return 0;
}

A B
0, 1, 2, 3, 4, 5 5
. .
C D
1, 2, 3, 4 6
. .

19. What will be the output of the program?


#include<stdio.h>
int main()
{
int i=3;
switch(i)
{
case 1:
printf("Hello\n");
case 2:
printf("Hi\n");
case 3:
continue;
default:
printf("Bye\n");
}
return 0;
}

A B
Error: Misplaced continue Bye
. .
C D
No output Hello Hi
. .

20. What will be the output of the program?


#include<stdio.h>
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default\n");
case 1:
printf("This is case 1\n");
break;
case 2:
printf("This is case 2\n");
break;
case 3:
printf("This is case 3\n");
}
return 0;
}

A This is default B This is case 3


. This is case 1 . This is default
C This is case 1 D
This is default
. This is case 3 .
21. The keyword used to transfer control from a function back to the calling
function is
A B
switch goto
. .
C D
go back return
. .

22. How many times the program will print "IndiaBIX" ?

#include<stdio.h>
int main()
{
printf("IndiaBIX");
main();
return 0;
}
A B
Infinite times 32767 times
. .
C D
65535 times Till stack overflows
. .

23. Input/output function prototypes and macros are defined in which header
file?
A B
conio.h stdlib.h
. .
C D
stdio.h dos.h
. .

24. What will be output if you compile and execute the above code?

int main()
{
char f=255.0;
printf("%d",sizeof(f++));
return 0;
}

(a)1
(b)8
(c)10
(d)Compiler error
25. What will be output if you compile and execute the above code?
#define int long
#define cal(x,y,z) x*y*z
int main(){
int a=2;
int b=cal(a++,a++,a++);
printf("%d,%d" ,a,b);
return 0;
}

(a)5,0
(b)5,125
(c)5,60
(d)Compiler error

Common questions

Powered by AI

Switch-case statements in C do not support floating-point type expressions or cases. This limitation is because switch expressions are evaluated to integer values, and only integral types are accepted for case labels. This constraint arises due to how switching evaluates to offsets in a constant set of possible values (such as integers). Therefore, only character and integer types can be used to define case labels, while floating-point types would cause a compilation error .

The expression 'n = a*a/i + i/2*t + 2 + t;' invokes implicit type conversion across different arithmetic operations. 'a*a/i' undergoes integer division because it involves only integer variables (a = 4, i = 3), resulting in 5 (integer division truncates fractions). Similarly, 'i/2*t' is first evaluated where 'i/2' results in 1 (truncated), making '1*t' equal to 4.2. Altogether added with '2 + t', the combined expression results in an implicit cast to float during execution before the resulting float is stored back into 'n', finally converted to an integer due to assignment to 'int n', thus truncating decimals .

The 'continue' keyword inside a 'switch' statement is misused in this context as it is designed to move the control back to the loop header, not to control flow within a switch-case. When placed in a switch structure, it causes a compilation error because 'continue' is not valid within 'switch'. It is meant for loop control, but here, it mistakenly suggests a continuation of the loop's next iteration, which leads to a compilation error with the message 'Misplaced continue,' halting the program .

Logical operators in C such as '||' (logical OR) and '&&' (logical AND) dictate the execution flow by governing condition evaluations. Operator precedence determines how conditions are grouped, with '&&' having higher precedence than '||'. In nested ternary operations like 'd > 1 || e > 1 ? 100:200', '||' ensures if either side of the condition is true, it bypasses subsequent AND evaluations. Additionally, operator precedence ensures innermost expressions are evaluated first, ensuring operational logic accurate to design but requiring careful orchestration for anticipated outcomes .

Pre-increment (++b) increases the variable's value before its current value is used in the expression, influencing the result of said expression in real-time. Conversely, post-increment (a++) uses the variable's current value in the expression before incrementing. In a nuanced assignment like 'd = a++ + ++b;', 'b' is incremented before addition, while 'a' is added as is and incremented afterward. The order directly determines the value assignments, affecting subsequent print and compute operations .

The conditional (ternary) operator '?:' allows for concise evaluation of boolean expressions, offering a shorthand method to perform if-else logic within single-line expressions. It conducts expanded conditional evaluations in reduced form by selecting one of two results (x) if the condition (expr) is true, another (y) if false. This streamlines code, reducing clutter by minimizing lengthy if-else blocks, hence optimizing decision-making processes and improving readability and maintenance in C programming .

The code recursively calls 'main()' within itself without a base case or a stop condition to break the recursion. This can lead to a stack overflow as each recursive call to 'main()' consumes stack space, eventually exhausting available memory. It violates best practices by not having an established termination condition, illustrating a common pitfall in recursive function design where each call continues infinitely (in theory), consuming increasingly more memory (stack frame) until system limits or crashes occur .

The ternary operator used in the code is 'a = c > 1 ? d > 1 || e > 1 ? 100 : 200 : 300;'. It evaluates three conditions based on logical comparisons. The first condition 'c > 1' is false since 'c' is set to 0, so the expression directly returns the 'else' part which is 300. Therefore, 'a' is assigned the value 300, and the printed result is 'a = 300' .

The 'sizeof' operator returns the size (bytes) of its operand. In the example 'sizeof('7')' returns 2 because the character literal is interpreted as a char (possibly signed or unsigned). 'sizeof(7)' returns 4 bytes as 7 is treated as an integer literal. 'sizeof(7.0)' results in 8 bytes since it's a double literal by default in C. This illustrates how 'sizeof' depends on data type, reflecting actual memory space taken for storing variable values or constants .

The key difference lies in the position and spacing of increment operators in the arithmetic operation. In Program 1, the expression 'd = a++ + ++b;' involves separate and valid increment operators, which correctly increments 'a' after the operation and increments 'b' before the operation. Thus, Program 1 compiles and runs without any issues. Program 2, however, has the expression 'd = a++ +++b;' where '+++' is interpreted as a concatenation of operators without proper separation or definition between them, leading to a syntax error. Thus, Program 2 does not compile successfully .

You might also like