© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

7. At which condition switch statement is used?

Ans:

When we need to choose one among number of alternatives, a switch statement is used

Ex - 1:  What is the output?
 {
unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");

}

a) same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT

Ans. (a)

Ex - 2:  What is the subtle error in the following code segment?
void fun(int n, int arr[])
{
int *p=0;
int i=0;
while(i++<n)
            p = &arr[i];
*p = 0;
}
Answer & Explanation:

If the body of the loop never executes p is assigned no address. So p remains NULL where *p =0 may result in problem (may rise to runtime error “NULL pointer assignment” and terminate the program).    

Ex - 3:  What is the output?
#include <stdio.h>
main()
{
char * str = "hello";
char * ptr = str;
char least = 127;
while (*ptr++)
                  least = (*ptr<least ) ?*ptr :least;
printf("%d",least);
}
Answer:
0
Explanation:

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. 


SLogix Student Projects
bottom