Earn 1860 Rs/Month by reading SMS
Today's Winner are
Deepak (from United College, Allahabad)
1. main()
{
int x=20;
{
int x=10;
printf(“%dn”,x);
}
printf(“%dn”,x);
}
Ans: 10
20
2. main()
{
int x=5;
x = x++;
printf (“%dn”,x);
}
Ans: 5
3. main()
{
int x=10;
printf(“%d %d”, ++x, ++x)
}
Ans: 12 11
4. What is the output of the following program if its command line argument were …
Sample 1 5 7
main(int argc, char *argv[])
{
int x;
x= argv[1] + argv[2] +argv[3]);
printf (“%d”, x);
}
Ans: Will show error because compiler treats 1 5 7 as characters array
5. # define VOLDEMORT he_who_must_not_be_names
main()
{
printf(“VOLDEMORT”);
}
Ans VOLDEMORT
Imprtant:
Send answer of Campus_11 (C Question) top 10 right answer sender's name will be published on main page of sandeeoinfo.com
so send your answer quickly.
Process to send answer
Send your answer along with your name and your college name to sandeepinfo@yahoo.co.in
Join http://groups.yahoo.com/group/campus_online/ for more updated information
Earn 1860 Rs/Month by reading SMS
Showing posts with label C. Show all posts
Showing posts with label C. Show all posts
Monday, May 14, 2007
Sunday, May 13, 2007
Campus_10 (C Question)
Find out the output of following:
1. main()
{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}
Answer : Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
2. main()
{
int m=- -2;
printf("m=%d",m);
}
Answer: m=2;
Explanation:
Here unary minus (or negation) operator is used twice.
maths rules applies, minus * minus= plus.
3. #define int char
main()
{
int k=65;
printf("sizeof(k)=%d",sizeof(k));
}
Answer: sizeof(k)=1
Explanation:
Since the #define replaces the string int by the macro char
4. main()
{
int m=10;
m=!m>14;
printf("m=%d",m);
}
Answer: m=0
5. #include
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *q,*str,*str1;
q=&s[3];
str=q;
str1=s;
printf("%d",++*q + ++*str1-32);
}
Answer: 77
Explanation:
q is pointing to character '\n'. str1 is pointing to character 'a' ++*q. "q 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 ++*q 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).
1. main()
{
char string[]="Hello World";
display(string);
}
void display(char *string)
{
printf("%s",string);
}
Answer : Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
2. main()
{
int m=- -2;
printf("m=%d",m);
}
Answer: m=2;
Explanation:
Here unary minus (or negation) operator is used twice.
maths rules applies, minus * minus= plus.
3. #define int char
main()
{
int k=65;
printf("sizeof(k)=%d",sizeof(k));
}
Answer: sizeof(k)=1
Explanation:
Since the #define replaces the string int by the macro char
4. main()
{
int m=10;
m=!m>14;
printf("m=%d",m);
}
Answer: m=0
5. #include
main()
{
char s[]={'a','b','c','\n','c','\0'};
char *q,*str,*str1;
q=&s[3];
str=q;
str1=s;
printf("%d",++*q + ++*str1-32);
}
Answer: 77
Explanation:
q is pointing to character '\n'. str1 is pointing to character 'a' ++*q. "q 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 ++*q 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).
Thursday, May 3, 2007
Campus_5( C Questions)
Predict the output or error(s) for the following:
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
2. main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].
3. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Answer:
I hate U
Explanation:
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .
4. main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Answer: 5 4 3 2 1
Explanation:When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.
5. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
Answer: 2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.
6. main()
{
extern int i;
i=20;
printf("%d",i);
}
Answer:
Linker Error : Undefined symbol '_i'
Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .
7. main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&amp;amp;j++&&k++l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Answer: 0 0 1 3 1
Explanation :
Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR () operator. So the expression ‘i++ &&amp; j++ && k++’ is executed first. The result of this expression is 0 (-1 &&amp; -1 && 0 = 0). Now the expression is 0 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
8. main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
Answer: 1 2
Explanation:
The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.
9. main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
Answer : three
Explanation :
The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
10. main()
{
printf("%x",-1<<4);
}
Answer: fff0
Explanation :
-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.
1. void main()
{
int const * p=5;
printf("%d",++(*p));
}
Answer:
Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".
2. main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Answer:
mmmm
aaaa
nnnn
Explanation:
s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea. Generally array name is the base address for that array. Here s is the base address. i is the index number/displacement from the base address. So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].
3. main()
{
float me = 1.1;
double you = 1.1;
if(me==you)
printf("I love U");
else
printf("I hate U");
}
Answer:
I hate U
Explanation:
For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.
Rule of Thumb:
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .
4. main()
{
static int var = 5;
printf("%d ",var--);
if(var)
main();
}
Answer: 5 4 3 2 1
Explanation:When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.
5. main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++) {
printf(" %d ",*c);
++q; }
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
Answer: 2 2 2 2 2 2 3 4 6 5
Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is incremented and not c , the value 2 will be printed 5 times. In second loop p itself is incremented. So the values 2 3 4 6 5 will be printed.
6. main()
{
extern int i;
i=20;
printf("%d",i);
}
Answer:
Linker Error : Undefined symbol '_i'
Explanation:
extern storage class in the following declaration,
extern int i;
specifies to the compiler that the memory for i is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name i is available in any other program with memory space allocated for it. Hence a linker error has occurred .
7. main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&amp;amp;j++&&k++l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Answer: 0 0 1 3 1
Explanation :
Logical operations always give a result of 1 or 0 . And also the logical AND (&&) operator has higher priority over the logical OR () operator. So the expression ‘i++ &&amp; j++ && k++’ is executed first. The result of this expression is 0 (-1 &&amp; -1 && 0 = 0). Now the expression is 0 2 which evaluates to 1 (because OR operator always gives 1 except for ‘0 0’ combination- for which it gives 0). So the value of m is 1. The values of other variables are also incremented by 1.
8. main()
{
char *p;
printf("%d %d ",sizeof(*p),sizeof(p));
}
Answer: 1 2
Explanation:
The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.
9. main()
{
int i=3;
switch(i)
{
default:printf("zero");
case 1: printf("one");
break;
case 2:printf("two");
break;
case 3: printf("three");
break;
}
}
Answer : three
Explanation :
The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.
10. main()
{
printf("%x",-1<<4);
}
Answer: fff0
Explanation :
-1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integer value be printed as a hexadecimal value.
Wednesday, May 2, 2007
Campus_4 (C,C++ questions)
Q: How many ways are there to initialize an int with a constant?
A: There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.int foo = 123;int bar (123);
Q: Explain the scope resolution operator.
A: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
The answer can get complicated. However, it should start with “::”. If the programmer is well into the design or use of classes that employ inheritance you might hear a lot about overriding member function overrides to explicitly call a function higher in the hierarchy. That’s good to know, but ask specifically about global scope resolution. You’re looking for a description of C++’s ability to override the particular C behavior where identifiers in the global scope are always hidden by like identifiers in a local scope.
Q: In C, why is the void pointer useful? When would you use it?
A: The void pointer is useful because it is a generic pointer that any pointer can be cast into and back again without loss of information.
Q: In C, what is the difference between a static variable and global variable?
A: A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).
Q: In C++, what is the difference between method overloading and method overriding?
A: Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
Q: What is pure virtual function?
A: A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration.
Q: What is a virtual destructor?
A: The simple answer is that a virtual destructor is one that is declared with the virtual attribute. The behavior of a virtual destructor is what is important. If you destroy an object through a pointer or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be complete.
Q: What is virtual channel?
A: Virtual channel is normally a connection from one source to one destination, although multicast connections are also permitted. The other name for virtual channel is virtual circuit.
Q: You have one base class virtual function how will call that function from derived class?
A: class a{public virtual int m(){return 1;}}
class b:a{public int j(){return m();}}
Q: Write a function that will reverse a string.
A: char *strrev(char *s)
{
int i = 0,
len = strlen(s);
char *str;
if ((str = (char *)malloc(len+1)) == NULL) /*cannot allocate memory */
err_num = 2;return (str);
}
while(len)str[i++]=s[?len];
str[i] = NULL;return (str);
}
A: There are two formats for initializers in C++ as shown in the example that follows. The first format uses the traditional C notation. The second format uses constructor notation.int foo = 123;int bar (123);
Q: Explain the scope resolution operator.
A: It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.
The answer can get complicated. However, it should start with “::”. If the programmer is well into the design or use of classes that employ inheritance you might hear a lot about overriding member function overrides to explicitly call a function higher in the hierarchy. That’s good to know, but ask specifically about global scope resolution. You’re looking for a description of C++’s ability to override the particular C behavior where identifiers in the global scope are always hidden by like identifiers in a local scope.
Q: In C, why is the void pointer useful? When would you use it?
A: The void pointer is useful because it is a generic pointer that any pointer can be cast into and back again without loss of information.
Q: In C, what is the difference between a static variable and global variable?
A: A static variable declared outside of any function is accessible only to all the functions defined in the same file (as the static variable). However, a global variable can be accessed by any function (including the ones from different files).
Q: In C++, what is the difference between method overloading and method overriding?
A: Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
Q: What is pure virtual function?
A: A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration.
Q: What is a virtual destructor?
A: The simple answer is that a virtual destructor is one that is declared with the virtual attribute. The behavior of a virtual destructor is what is important. If you destroy an object through a pointer or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be complete.
Q: What is virtual channel?
A: Virtual channel is normally a connection from one source to one destination, although multicast connections are also permitted. The other name for virtual channel is virtual circuit.
Q: You have one base class virtual function how will call that function from derived class?
A: class a{public virtual int m(){return 1;}}
class b:a{public int j(){return m();}}
Q: Write a function that will reverse a string.
A: char *strrev(char *s)
{
int i = 0,
len = strlen(s);
char *str;
if ((str = (char *)malloc(len+1)) == NULL) /*cannot allocate memory */
err_num = 2;return (str);
}
while(len)str[i++]=s[?len];
str[i] = NULL;return (str);
}
Subscribe to:
Posts (Atom)