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;
}
}
|
||||||||||
The correct output for the following program is:
void main( ) {
int c=- -2;
printf("c = %d",c);
}
|
|||||||||||
Pick the right option for the following code part:
#define int char
void main(
) {
int a = 65;
printf( "sizeof( a ) =
%d", sizeof( a ) );
}
|
|||||||||||
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);
}
|
|||||||||||
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 );
}
|
|||||||||||
The output of the following program is:
void main( ){
char
str[ 4 ] = "HELLO";
printf(
"%s", str );
}
|
|||||||||||
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 );
}
|
|||||||||||
The right output of the following program is:
void main( ){
int i=300;
char *ptr = &i;
*++ptr=2;
printf("%d",i);
}
|
|||||||||||
Chose the right output for the following program:
void main( ){
int
i = 258;
int
*iPtr = &i;
printf("%d
%d", *((char*)iPtr), *((char*)iPtr+1) );
}
|
|||||||||||
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 ) );
}
|
Answers
251.
|
d
|
The default case can be placed anywhere inside the
loop. It is executed only when all other cases doesn't match.
|
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.
|
|
c
|
Since the #define replaces the string int
by the macro char.
|
|
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)
|
|
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
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
thank you.
ReplyDeletec++ tutorial
java tutorial