Instruction
- Total number of questions : 20
- Time allotted : 30:00 Minutes:Seconds.
- Each question carry 1 mark, no negative marks.
- DO NOT refresh the page.
- All the best
C Programming [ Question No: 1 To 20 ] :
What is the output of the following 'C' language statements?
#include\(\lt\)stdio.h\(\gt\)
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before : **p1=%d **p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter : **p1=%d **p2=%d",*p1,*p2);
return 0;
}
What is the output of this C code?
#include <stdio.h>
int main()
{
printf("sanfoundry\r\nclass\n");
return 0;
}
To print out a and b given below, which printf() statement would you use?
float a = 3.14;
double b = 3.14;
In case of ordinary int variables
What will be the output of the program?
#include <stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *str1="India";
char *str2="BIX";
JOIN(str1, str2);
return 0;
}
Identify the macro(s) defined in stdarg.h.
Can we increase the size of statically allocated array?
What will be the output of the following C code?
#include <stdio.h>
typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}
What does the second parameter of the main function represent?
A program that has no command line arguments will have argc _________
What is Array?
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include <stdio.h>
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
What will be the output of the program?
#include <stdio.h>
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}
In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
What will be the output of the program?
#include <stdio.h>
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
The operator used to get value at address stored in a pointer variable is
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
Which of the following is the correct syntax to send an array as a parameter to function?
The scope of an automatic variable is:
Algorithm and Flow chart help us to
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20
C Programming [ Question No: 1 To 20 ] :
What is the output of the following 'C' language statements?
#include\(\lt\)stdio.h\(\gt\)
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before : **p1=%d **p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter : **p1=%d **p2=%d",*p1,*p2);
return 0;
}
Your Answer : (Not Answered)
Correct Answer : A
The output is Before : **p1=10 **p2=20 and then After : **p1=20 **p2=10. The pointers p1 and p2 point to a and b, so the three statements swap the values without a temporary variable. \[ *p1 = 10 + 20 = 30 \] \[ *p2 = 30 - 20 = 10 \] \[ *p1 = 30 - 10 = 20 \] The strings '**p1=' and '**p2=' are printed as plain text. The second option reverses the values, the third drops the asterisks that the format string prints, and the fourth gives the wrong second value.
What is the output of this C code?
#include <stdio.h>
int main()
{
printf("sanfoundry\r\nclass\n");
return 0;
}
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: sanfoundry class
Here the carriage return is followed by a newline. The carriage return moves the cursor to the start of the line and the newline moves it down one, so nothing is overwritten and the two words appear on separate lines.
Compare this with the same code using \r alone, which prints classundry because the second word overwrites the first. The pair \r\n is the conventional line ending on some systems, and its combined effect is simply to end the line.
To print out a and b given below, which printf() statement would you use?
float a = 3.14;
double b = 3.14;
Your Answer : (Not Answered)
Correct Answer : A
Correct answer: printf("%f%f",a,b);
Both are printed with %f. In a call to a variadic function such as printf, the default argument promotions apply, and a float argument is promoted to double before it is passed - so printf receives two doubles and %f is correct for each.
There is therefore no %f for float and separate specifier for double; %f serves both. %Lf is for long double, and using it here would misread the argument and produce nonsense.
In case of ordinary int variables
Your Answer : (Not Answered)
Correct Answer : A
Correct answer: leftmost bit is reserved for sign
A plain int is signed, and in the two's complement representation used by essentially every current implementation the most significant - leftmost - bit carries the sign: 0 for non-negative, 1 for negative.
That bit is not available for magnitude, which is why a 32-bit int spans roughly \( -2^{31} \) to \( 2^{31}-1 \) rather than 0 to \( 2^{32}-1 \). Declaring the variable unsigned releases the bit for magnitude and doubles the positive range.
What will be the output of the program?
#include <stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
char *str1="India";
char *str2="BIX";
JOIN(str1, str2);
return 0;
}
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: str1=India str2=BIX
Each parameter is used twice: once with #, which stringizes the argument’s text, and once plainly, which yields the pointer. So #s1 becomes "str1" while s1 is the variable holding "India". The output is str1=India str2=BIX - the idiom used by debugging macros that print both a variable’s name and its value.
Identify the macro(s) defined in stdarg.h.
Your Answer : (Not Answered)
Correct Answer : D
Correct answer: all the above
<stdarg.h> defines all three: va_start to begin walking the variable argument list, va_arg to fetch the next argument (given its type), and va_end to finish. They are what a function such as printf uses to reach arguments the compiler cannot type-check.
Can we increase the size of statically allocated array?
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: No
A statically allocated array has its size fixed at compile time, in the stack frame or the data segment, and there is no mechanism to change it while the program runs.
The only way to get a larger array is to allocate a new one dynamically and copy the contents across - which is precisely what realloc does for heap blocks. This inflexibility is the central reason dynamic allocation exists, and it is why growable structures are always built on the heap.
What will be the output of the following C code?
#include <stdio.h>
typedef struct p
{
int x, y;
}k;
int main()
{
struct p p = {1, 2};
k k1 = p;
printf("%d\n", k1.x);
}
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: 1
Here everything is used correctly. k is an alias for struct p; struct p p = {1, 2}; declares and initialises a structure, the variable name p shadowing the tag harmlessly since tags live in their own namespace; and k k1 = p; copies it, structure assignment being a member-by-member copy. So k1.x is 1.
What does the second parameter of the main function represent?
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: List of command line arguments
The second parameter is argv, an array of pointers to the argument strings - one pointer per argument, in the order typed.
argv[0] holds the program name and argv[argc] is guaranteed to be a null pointer, so the list can be walked either by counting to argc or by stopping at the null. The count is the first parameter, and C has no dictionary or stack type to offer.
A program that has no command line arguments will have argc _________
Your Answer : (Not Answered)
Correct Answer : C
Correct answer: One
The program name occupies argv[0] and is counted, so argc is 1 when nothing else is supplied.
That is why a program testing for arguments writes if (argc > 1) rather than if (argc). The standard permits argc to be 0, in which case argv[0] is a null pointer, but this happens only where a program is started by an exec call that supplies no name at all - never from an ordinary shell.
What is Array?
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: Collection of similar type of elements
An array is a collection of elements of the same type, stored consecutively and reached by index. The uniform element size is what makes the index arithmetic work; a collection of mixed types is a structure, not an array.
What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include <stdio.h>
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
printf("%u, %u\n", a+1, &a+1);
return 0;
}
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: 65480, 65496
a is a 3×4 array of 2-byte ints. a alone decays to a pointer to its first row, so adding 1 skips one row of 4 ints:
&a is a pointer to the whole array, so adding 1 skips all 12 ints:
Same address, different type - and the type is what decides how far +1 moves.
What will be the output of the program?
#include <stdio.h>
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}
Your Answer : (Not Answered)
Correct Answer : A
Correct answer: 2 1 3 1 4 1 5 1 6 1 7 0
The third clause of the for is the printf, so the values are printed after each pass through the body. The body sets y to the result of x++ <= 5, which is 1 while the old x is at most 5. So the pairs printed are 2 1, 3 1, 4 1, 5 1, 6 1 - and on the pass where x is 6 the comparison fails, giving 7 0, after which the condition y is false and the loop ends.
In _______, the bodies of the two loops are merged together to form a single loop provided that they do not make any references to each other.
Your Answer : (Not Answered)
Correct Answer : D
Correct answer: Loop jamming
Loop jamming (loop fusion) merges two loops with the same trip count into one, which is safe only if neither depends on the other’s results. The gain is one set of loop overheads instead of two, and better use of the cache, since the data is walked once. Loop unrolling repeats the body to cut the number of tests; strength reduction replaces an expensive operation with a cheaper one.
What will be the output of the program?
#include <stdio.h>
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: 12480
Trace the loop. c is 48, which is \( 110000_2 \), the character '0'. Each pass ORs it with the mask and prints the result as a character, then shifts the mask left.
Masks 1, 2, 4 and 8 set bits that 48 does not have, giving 49, 50, 52 and 56 - the characters 1, 2, 4 and 8.
The fifth mask is 16, and 48 is \( 32 + 16 \), so bit 4 is already set: OR changes nothing and 48 prints as 0. The output is 12480, and the final zero is the whole point - OR is idempotent.
The operator used to get value at address stored in a pointer variable is
Your Answer : (Not Answered)
Correct Answer : A
Correct answer: *
The unary * is the indirection, or dereference, operator: it yields the object a pointer points to. Its counterpart & is the address-of operator, going the other way.
The asterisk plays two distinct roles that are easy to conflate. In a declaration it forms a pointer type; in an expression it dereferences. The && and || operators are logical AND and OR and have nothing to do with pointers.
What would be the equivalent pointer expression for referring the array element a[i][j][k][l]
Your Answer : (Not Answered)
Correct Answer : B
Correct answer: *(*(*(*(a+i)+j)+k)+l)
Each subscript is defined as a dereference of a sum: x[y] means *(x+y). Applying that rule four times, from the outside in, gives one dereference for every subscript.
\[ a[i][j][k][l] \equiv *(*(*(*(a+i)+j)+k)+l) \]
The options without the asterisks compute addresses but never read through them. Note that the pointer arithmetic is scaled by the size of the sub-array at each level, which is why the array's dimensions must be known to the compiler.
Which of the following is the correct syntax to send an array as a parameter to function?
Your Answer : (Not Answered)
Correct Answer : A
Correct answer: func(&array);
Of the four forms offered this is the only valid C that passes the array's address. &array yields a pointer to the whole array, which the function can receive as int (*)[N].
It is worth noting that the idiomatic form, func(array), is not among the options: an array name in an expression decays to a pointer to its first element, so writing the bare name already passes the address. The other options are not valid syntax at all, or pass a single element rather than the array.
The scope of an automatic variable is:
Your Answer : (Not Answered)
Correct Answer : D
Correct answer: Both (a) and (b)
An automatic variable is visible from its declaration to the end of the block that contains it, and that includes any blocks nested inside - unless one of them declares another variable of the same name, which shadows it.
Algorithm and Flow chart help us to
Your Answer : (Not Answered)
Correct Answer : D
Correct answer: Specify the problem completely and clearly
An algorithm sets out the steps of a solution in order, and a flowchart shows the same thing diagrammatically. Both exist to make the logic explicit before any code is written - which is where ambiguities and missing cases come to light. Neither has anything to say about memory, number bases or printers.
Marks
Total number of questions : 20
Number of answered questions : 0
Number of unanswered questions : 20