C-Programming
Question 1 |
# include <stdio.h>
int main()
{
int i, j, count;
count = 0;
i = 0;
for (j = -3; j <= 3; j++)
{
if ((j >= 0) && (i++))
count = count + j;
}
count = count + i;
printf(“%d”, count);
return 0;
}
Which one of the following options is correct?
The program will compile successfully and output 13 when executed. | |
The program will compile successfully and output 10 when executed. | |
The program will compile successfully and output 8 when executed. | |
The program will not compile successfully. |
Input: count=0 , i=0 and j=-3
For(j = -3; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition fails because they are given logical AND.
So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.
For(j = -2; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition fails because they are given logical AND.
So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.
For(j = -1; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition fails because they are given logical AND.
So, we are not entering the “if” condition and come out of the loop. The count and “i” values remain “0”.
For(j = 0; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition TRUE then enters into the loop.
count=0+0 → Count=0
For(j = 1; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition TRUE then enters into the loop.
count=0+1 → Count=1
For(j = 2; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition TRUE then enters into the loop.
count=1+2 → Count=3
For(j = 3; j <= 3; j++) → Condition TRUE then enters into the loop.
if((j >= 0) && (i++)) → Condition TRUE then enters into the loop.
count=3+3 → Count=6
For(j = 4; j <= 3; j++) → Condition FALSE we are not entering the loop.
count=6+4 → We are given a condition as a post increment. So, “i” updates the next instruction.
The above code segment executes successfully and will print value=10.
Question 2 |
What will be the output of the following C program segment?
char inchar = 'A'; switch (inchar) { case 'A' : printf ("choice A n") ; case 'B' : printf ("choice B ") ; case 'C' : case 'D' : case 'E' : default: printf ("No Choice") ; }
No Choice | |
Choice A | |
Program gives no output as it is erroneous |
So,
→ Choice A
→ Choice B. No choice. Is the output.
Question 3 |
Consider the following C program
int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }
What output will be generated by the given code segment?
Hence
4 2
6 2
2 0
Question 4 |
Consider the following C program
int a, b, c = 0; void prtFun (void); int main () { static int a = 1; /* line 1 */ prtFun(); a += 1; prtFun(); printf ( "n %d %d " , a, b) ; } void prtFun (void) { static int a = 2; /* line 2 */ int b = 1; a += ++b; printf (" n %d %d " , a, b); }
What output will be generated by the given code segment if:
- Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
Line 2 replaced by register int a=2;
In main there will be no change if it is static or auto because of a+=1 the auto variable a is updated to 2 from 1.
In prtfun ( ), register makes a difference.
For first print statement a is updated to 4 & prints 4, 2.
But now it is not a static variable to retain the value of a to 4. So it becomes 2, when second function call takes place & prints 4, 2 again. There is no change in b, it acts like a local variable.
Hence,
4 2
4 2
2 0.
Question 5 |
Consider the following C function definition.
int Trial (int a, int b, int c) { if ((a >= b) && (c < b) return b; else if (a >= b) return Trial(a, c, b); else return Trial(b, a, c); }
The function Trial:
Finds the maximum of a, b, and c | |
Finds the minimum of a, b and c | |
Finds the middle number of a, b, c | |
None of the above |
Question 6 |
The following C declarations
struct node { int i; float j; }; struct node *s[10];
define s to be
An array, each element of which is a pointer to a structure of type node | |
A structure of 2 fields, each field being a pointer to an array of 10 elements | |
A structure of 3 fields: an integer, a float, and an array of 10 elements | |
An array, each element of which is a structure of type node |
Question 7 |
Consider the following C declaration
struct { short s [5] union { float y; long z; } u; } t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is
22 bytes | |
14 bytes | |
18 bytes | |
10 bytes |
max[float, long] = max [4, 8] = 8
Total = short[5] + max[float,long] = 10 + 8 = 18
Question 8 |
The value of j at the end of the execution of the following C program.
int incr(int i) { static int count = 0; count = count + i; return (count); } main() { int i,j; for (i = 0; i <= 4; i++) j = incr(i); }
is
10 | |
4 | |
6 | |
7 |
i=1; count=1
i=2; count=3
i=3; count=6
i=4; count=10
It return count value is 10.
Question 9 |
Consider the following C program:
void abc(char*s) { if(s[0]==’\0’)return; abc(s+1); abc(s+1); printf(“%c”,s[0]); } main() { abc(“123”) }
(a) What will be the output of the program?
(b) If abc(s) is called with a null-terminated string s of length n characters (not
counting the null (‘\0’) character), how many characters will be printed by abc(s)?
Theory Explanation is given below. |
Question 10 |
In the C language
At most one activation record exists between the current activation record and the activation record for the main | |
The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence. | |
The visibility of global variables depends on the actual function calling sequence. | |
Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive fraction can be called. |
Question 11 |
In the following C program fragment, j, k n and TwoLog_n are interger variables, and A is an array of integers. The variable n is initialized to an integer ≥3, and TwoLog_n is initialized to the value of 2*⌈log2(n)⌉
for (k = 3; k < = n; k++) A[k] = 0; for (k = 2; k < = TwoLog_n; k++) for (j = k + 1; j < = n; j++) A[j] = A[j] || (j%k); for (j = 3; j < = n; j++) if (!A[j]) printf("%d", j);
The set of numbers printed by this program fragment is
{m|m ≤ n, (∃i)[m=i!]} | |
{m|m ≤ n, (∃i)[m=i2]} | |
{m|m ≤ n, m is prime}
| |
{ } |
Now Trace the code,
for (k=3; k<=n; k++)
A[k]=0; // A[3]=0
A[4]=0
for (k=2; k<=Two log_n; k++)
for(j=k+1; j<=n; j++)
A[j] = A[j] // (j%k); // A[3] = 0 // I=1
A[4] = 0 // I=1
for (j=3; j<=n; j++)
if (!A[j]) printf("%d", j);
// if (!1) means if (0), so printf will never execute
Hence, Option (D) is the answer.
Question 12 |
Consider the C program shown below.
#include#define print(x) printf("%d", x) int x; void Q(int z) { z += x; print(z); } void P(int *y) { int x = *y + 2; Q(x); *y = x - 1; print(x); } main(void) { x = 5; P(&x); print(x); }
The output of this program is
12 7 6
| |
22 12 11 | |
14 6 6 | |
7 6 6 |
p(&x) it goes to P( ) function
y=5
x=5+2=7;
Q(x)
z=7
z=7+5=12(Print+z→I)
comes to P( )
*y=7-1=6
x=7(Print x→II)
comes to main ( ),
print x=*y=6 (print x→III)
Output: 12 7 6
Question 13 |
Consider the C program given below:
#includeint main () { int sum = 0, maxsum = 0, i, n = 6; int a [] = {2, -2, -1, 3, 4, 2}; for (i = 0; i < n; i++) { if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) { if (sum > maxsum) maxsum = sum; sum = (a [i] > 0) ? a [i] : 0; } else sum += a [i]; } if (sum > maxsum) maxsum = sum ; printf ("%dn", maxsum); }
What is the value printed out when this program is executed?
9 | |
8 | |
7 | |
6 |