C Programming and Problem Solving Questions and Answers 251 to 260

C Programming and Problem Solving

Questions 251 to 260



251.
The output of the following program is:
main( ){
int i=3;
switch(i)  {
default: printf("zero");
case 1:               printf("one"); break;
case 2: printf("two"); break;
case 3:               printf("three"); break;
}
}
(a)
zero
(b)
zero one
(c)
one
(d)
three
(e)
Compilation error.
252.
The correct output for the following program is:
void main( ) {
int c=- -2;
printf("c = %d",c);
}
(a)
c = -2
(b)
c = 2
(c)
c = 1
(d)
Syntax error
(e)
c = -1.
253.
Pick the right option for the following code part:
#define int char
void main( ) {
int a = 65;
printf( "sizeof( a ) = %d", sizeof( a ) );
}
(a)
sizeof( a ) = 2
(b)
sizeof( a ) = 4
(c)
sizeof( a ) = 1
(d)
sizeof( a ) = 65
(e)
Syntax error.
254.
What do you say about the output of the following program?
#include<stdio.h>
void main( ) {
char s[]={'a','b','c','\n','c','\0'};
char *ptr,*str,*str1;
ptr=&s[3];
str=ptr;
str1=s;
printf("%d",++*ptr + ++*str1-32);
}
(a)
97
(b)
98
(c)
Runtime error
(d)
78
(e)
77.
255.
The output of the following program is:
void main(){
int a = 10, *j;
void *k;
j = k = &a;
j++, k++;
printf( "\n %u %u ", j, k );
}
(a)
Compiler error
(b)
Runtime error
(c)
10
(d)
11
(e)
Address of a is incremented.
256.
The output of the following program is:
void main( ){
char str[ 4 ] = "HELLO";
printf( "%s", str );
}
(a)
Runtime error
(b)
Compiler Error
(c)
HELLO
(d)
HELL
(e)
HELL and some garbage characters.
257.
The correct output of the following program is:
#include <stdio.h>
void main( ) {
char * str = "hello";
char * ptr = str;
char least = 127;
while ( *ptr++ )
least = ( *ptr<least ) ? *ptr : least;
printf( "%d", least );
}
(a)
No output
(b)
    0
(c)
104
(d)
111
(e)
  48.
258.
The right output of the following program is:
void main( ){
int i=300;
char *ptr = &i;
*++ptr=2;
printf("%d",i);
}
(a)
553
(b)
554
(c)
555
(d)
556
(e)
No output at all.
259.
Chose the right output for the following program:
void main( ){
int i = 258;
int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}
(a)
Compiler Error
(b)
Runtime Error
(c)
258 257
(d)
2 2
(e)
2 1.
260.
What is the return value of the function ‘Fun( )’.
int Fun(int a[]) { return sizeof(a)/sizeof(int ); }
void main( ) {
int array[10];
printf( "The dimension of the array is %d", Fun( array ) );
}
(a)
10
(b)
  2
(c)
  1
(d)
20
(e)
Syntax error.



Answers




251.
d
The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
252.
b
Here unary minus (or negation) operator is used twice. Same math’s rules applies, ie. minus * minus= plus. However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg. i--). 2 is a constant and not a variable.
253.
c
Since the #define replaces the string  int by the macro char.
254.
e
ptr is pointing to character '\n'. str1 is pointing to character 'a' ++*ptr. "ptr is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then incremented to 11. The value of ++*ptr is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.  Now performing (11 + 98 – 32), we get 77("M");  So we get the output 77. "M" (Ascii is 77)
255.
a
Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers. Compiler error: Cannot increment a void pointer
256.
b
The array str is of size 4 but the string constant requires 6 bytes to get stored. The error message is Compiler error: Too many initializers.
257.
b
After ‘ptr’ reaches the end of the string the value pointed by ‘str’ is ‘\0’. So the value of ‘str’ is less than that of ‘least’. So the value of ‘least’ finally is 0.
258.
d
The integer value 300 in binary notation is: 00000001 00101100. It is  stored in memory (small-endian) as: 00101100 00000001. Result of the expression *++ptr = 2 makes the memory representation as: 00101100 00000010. So the integer corresponding to it is 00000010 00101100 => 556.
259.
e
The integer value 257 can be represented in binary as, 00000001 00000001. Remember that the INTEL machines are ‘small-endian’ machines. Small-endian means that the lower order bytes are stored in the higher memory addresses and the higher order bytes are stored in lower addresses. The integer value 258 is stored in memory as: 00000001 00000010
260.
c
Arrays cannot be passed to functions as arguments and only the pointers can be passed. So the argument is equivalent to int * array. The return statement becomes, sizeof(int *)/ sizeof(int) that happens to be 1 in this case.


<< Prev 1...  2...  3...  4...  5...  6...  7...  8...  9...  10...  11... 12...  13...  14...  15...  16...  17...  18...  19...  20...  21...  22...  23...  24...  25...  26...  27...  28...  29...  30...  31...  32...  33...  34...  35...  36...  Next >>


1 comment :