Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "GPLgtests"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to pstr[0] = "Hello", pstr[1] = "GPLgtests"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s
%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].
Hence the output of the program is : Hello GPLgtests
Q. No. :
6
Question :
What will be the output of the program?
#include< stdio.h >
int check (int, int);
int main()
{
int c;
c = check(10, 20);
printf("c=%d
", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=&i;
q=&j;
i>=45 ? return(*p): return(*q);
}
int a=250; The variable a is declared as an integer type and initialized to value 250. printf("%1d
", a); It prints the value of variable a. Hence the output of the program is 250.
Q. No. :
11
Question :
Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?
The scope of variable 'j' is the single printf that follows it. So, the last statement that involves 'j' will complain about the undeclared identifier 'j'
Q. No. :
15
Question :
The following code fragment
int x,y = 2,z,a;
x = (y *= 2) + (z = a = y);
printf("%d",x);
A :
prints 8
B :
prints 6
C :
prints 6 or 8 d3epending on the compiler implementation
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);. The function runs infinetely because the there is a post-increment operator is used. So, it calls reverse(5) infinitely. loops
Q. No. :
19
Question :
Which of the following function is correct that finds the length of a string?
Step 1: int fun(int); Here we declare the prototype of the function fun().
Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.
Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increment operator.
Step 4: Then the control back to the main function and the value 10 is assigned to variable i.
Step 5: printf("%d
", --i); Here --i denoted pre-increment. Hence it prints the value 9.
The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values. Step 2: printf("%d
", sizeof(arr)/sizeof(arr[0])); The variable arr has 4 elements. The size of the float variable is 4 bytes. Hence 4 elements x 4 bytes = 16 bytes sizeof(arr[0]) is 4 bytes Hence 16/4 is 4 bytes Hence the output of the program is '4'.
Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello". We have use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".
Q. No. :
24
Question :
What will be the output of the program if it is executed like below?
cmd> sample
The logical not operator takes expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it reverses the value of the expression.
Step 1: if(!(!x) && x)
Step 2: if(!(!10) && 10)
Step 3: if(!(0) && 10)
Step 3: if(1 && 10)
Step 4: if(TRUE) here the if condition is satisfied. Hence it prints x = 10.
Q. No. :
30
Question :
What will be the output of the program?
#include< stdio.h >
int X=40;
int main()
{
int X=20;
printf("%d
", X);
return 0;
}
Step 1: x=y=z=1; here the variables x ,y, z are initialized to value '1'.
Step 2: z = ++x || ++y && ++z; becomes z = ( (++x) || (++y && ++z) ). Here ++x becomes 2. So there is no need to check the other side because ||(Logical OR) condition is satisfied.(z = (2 || ++y && ++z)). There is no need to process ++y && ++z. Hence it returns '1'. So the value of variable z is '1'
Step 3: printf("x=%d, y=%d, z=%d
", x, y, z); It prints "x=2, y=1, z=1". here x is increemented in previous step. y and z are not increemented.
Q. No. :
36
Question :
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
A :
The element will be set to 0.
B :
The compiler would report an error.
C :
The program may crash if some important data gets overwritten.
if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than double constant 0.7. Hence the if condition is satisfied and it prints 'C'
Example:
#include< stdio.h >
int main()
{
float a=0.7;
printf("%.10f %.10f
",0.7, a);
return 0;
}
Output:
0.7000000000 0.6999999881
Q. No. :
41
Question :
If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';
Hence the output of the program is 24, 5
Q. No. :
44
Question :
Which of the following statements are correct about the below C-program?
#include< stdio.h >
int main()
{
int x = 10, y = 100%90, i;
for(i=1; i<10; i++)
if(x != y);
printf("x = %d y = %d
", x, y);
return 0;
}
1 : The printf() function is called 10 times.
2 : The program will produce the output x = 10 y = 10
3 : The ; after the if(x!=y) will NOT produce an error.
4 : The program will not produce output.