Friday 13 July 2012

Interview Question...Practice


1.
We want to round off x, a float, to an int value, The correct way to do is
A.
y = (int)(x + 0.5)
B.
y = int(x + 0.5)
http://www.indiabix.com/_files/images/website/wrong.gif
C.
y = (int)x + 0.5
D.
y = (int)((int)x + 0.5)
Answer: Option A
Explanation:
Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.
y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)
Example:

#include <stdio.h>

int main ()
{
  float x = 3.6;
  int y = (int)(x + 0.5);
  printf ("Result = %d\n", y );
  return 0;
}
Output:
Result = 4.
Learn more problems on : Floating Point Issues
Discuss about this problem : Discuss in Forum


2.
Every function must return a value
A.
Yes
B.
No
http://www.indiabix.com/_files/images/website/accept.png
Answer: Option B
Explanation:
No, If a function return type is declared as void it cannot return any value.
Learn more problems on : Functions
Discuss about this problem : Discuss in Forum

3.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    FILE *ptr;
    char i;
    ptr = fopen("myfile.c", "r");
    while((i=fgetc(ptr))!=NULL)
        printf("%c", i);
    return 0;
}
A.
Print the contents of file "myfile.c"
B.
Print the contents of file "myfile.c" upto NULL character
C.
Infinite loop
http://www.indiabix.com/_files/images/website/accept.png
D.
Error in program
Answer: Option C
Explanation:
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
Learn more problems on : Input / Output
Discuss about this problem : Discuss in Forum

4.
What will be the output of the program?
#include<stdio.h>
int main()
{
    char str[]="C-program";
    int a = 5;
    printf(a >10?"Ps\n":"%s\n", str);
    return 0;
}
A.
C-program
B.
Ps
C.
Error
http://www.indiabix.com/_files/images/website/wrong.gif
D.
None of above
Answer: Option A
Explanation:
Step 1: char str[]="C-program"; here variable str contains "C-program".
Step 2: int a = 5; here variable a contains "5".
Step 3: printf(a >10?"Ps\n":"%s\n", str); this statement can be written as

if(a > 10)
{
    printf("Ps\n");
}
else
{
    printf("%s\n", str);
}
Here we are checking a > 10 means 5 > 10. Hence this condition will be failed. So it prints variable str.
Hence the output is "C-program".
Learn more problems on : Control Instructions
Discuss about this problem : Discuss in Forum

5.
Preprocessor directive #ifdef .. #else ... #endif is used for conditional compilation.
A.
True
http://www.indiabix.com/_files/images/website/accept.png
B.
False
Answer: Option A
Explanation:
True, these macros are used for conditional operation.

#if <constant-expression>
#elif <constant-expression>
#endif
Learn more problems on : C Preprocessor
Discuss about this problem : Discuss in Forum

6.
Which of the following statements correct about the below program?
#include<stdio.h>

int main()
{
    struct emp
    {
        char name[25];
        int age;
        float sal;
    };
    struct emp e[2];
    int i=0;
    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, &e[i].age, &e[i].sal);

    for(i=0; i<2; i++)
        scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
    return 0;
}
A.
Error: scanf() function cannot be used for structures elements.
B.
The code runs successfully.
http://www.indiabix.com/_files/images/website/wrong.gif
C.
Error: Floating point formats not linked Abnormal program termination.
D.
Error: structure variable must be initialized.
Answer: Option C
Explanation:
Refer the explanation given for another problem:
Learn more problems on : Structures, Unions, Enums
Discuss about this problem : Discuss in Forum

7.
Is there any difference int the following declarations?
int fun(int arr[]);
int fun(int arr[2]);
A.
Yes
B.
No
http://www.indiabix.com/_files/images/website/accept.png
Answer: Option B
Explanation:
No, both the statements are same. It is the prototype for the function fun() that accepts one integer array as an parameter and returns an integer value.
Learn more problems on : Arrays
Discuss about this problem : Discuss in Forum

8.
What will be the output of the program?
#include<stdio.h>
int i;
int fun();

int main()
{
    while(i)
    {
        fun();
        main();
    }
    printf("Hello\n");
    return 0;
}
int fun()
{
    printf("Hi");
}
A.
Hello
B.
Hi Hello
http://www.indiabix.com/_files/images/website/wrong.gif
C.
No output
D.
Infinite loop
Answer: Option A
Explanation:
Step 1: int i; The variable i is declared as an integer type.
Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.
Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.
Step 1: printf("Hello\n"); It prints "Hello".
Hence the output of the program is "Hello".
Learn more problems on : Functions
Discuss about this problem : Discuss in Forum

9.
What will be the output of the program?
#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);

int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}
A.
6
http://www.indiabix.com/_files/images/website/wrong.gif
B.
1
C.
0
D.
-1
Answer: Option B
Learn more problems on : Functions
Discuss about this problem : Discuss in Forum

10.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    char sentence[80];
    int i;
    printf("Enter a line of text\n");
    gets(sentence);
    for(i=strlen(sentence)-1; i >=0; i--)
        putchar(sentence[i]);
    return 0;
}
A.
The sentence will get printed in same order as it entered
B.
The sentence will get printed in reverse order
C.
Half of the sentence will get printed
http://www.indiabix.com/_files/images/website/wrong.gif
D.
None of above
Answer: Option B
Learn more problems on : Strings
Discuss about this problem : Discuss in Forum

11.
What will be the output of the program (Turbo C in 16 bit platform DOS) ?
#include<stdio.h>
#include<string.h>

int main()
{
    char *str1 = "India";
    char *str2 = "BIX";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}
A.
IndiaBIX India
B.
IndiaBIX IndiaBIX
C.
India India
D.
Error
http://www.indiabix.com/_files/images/website/wrong.gif
Answer: Option B
Explanation:
It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform).
It may cause a 'segmentation fault error' in GCC (32 bit platform).
Learn more problems on : Strings
Discuss about this problem : Discuss in Forum

12.
What will be the output of the program ?
#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}
A.
5
B.
25
C.
125
http://www.indiabix.com/_files/images/website/wrong.gif
D.
Garbage value
Answer: Option B
Learn more problems on : Pointers
Discuss about this problem : Discuss in Forum

13.
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
    int i;
    for(i=1; i<=3; i++)
        printf("%u\n", &argv[i]);
    return 0;
}
If the first value printed by the above program is 65517, what will be the rest of output?
A.
65525 65531
B.
65519 65521
http://www.indiabix.com/_files/images/website/accept.png
C.
65517 65517
D.
65521 65525
Answer: Option B
Learn more problems on : Command Line Arguments
Discuss about this problem : Discuss in Forum

14.
Which of the following cannot be checked in a switch-case statement?
A.
Character
B.
Integer
C.
Float
http://www.indiabix.com/_files/images/website/accept.png
D.
enum
Answer: Option C
Explanation:
The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.

switch( expression )
{
    case constant-expression1:    statements 1;
    case constant-expression2:    statements 2;   
    case constant-expression3:    statements3 ;
    ...
    ...
    default : statements 4;
}
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.
Learn more problems on : Control Instructions
Discuss about this problem : Discuss in Forum

15.
Point out the error in the program?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    fseek(fp, "20", SEEK_SET);
    fclose(fp);
    return 0;
}
A.
Error: unrecognised Keyword SEEK_SET
B.
Error: fseek() long offset value
C.
No error
D.
None of above
http://www.indiabix.com/_files/images/website/wrong.gif
Answer: Option B
Explanation:
Instead of "20" use 20L since fseek() need a long offset value.
Learn more problems on : Input / Output
Discuss about this problem : Discuss in Forum

16.
What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>

int main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("%s\n", e1.n);
    return 0;
}
A.
Error: Invalid structure assignment
B.
DRAVID
http://www.indiabix.com/_files/images/website/accept.png
C.
Dravid
D.
No output
Answer: Option B
Learn more problems on : Structures, Unions, Enums
Discuss about this problem : Discuss in Forum

17.
What do the following declaration signify?
char **argv;
A.
argv is a pointer to pointer.
B.
argv is a pointer to a char pointer.
C.
argv is a function pointer.
http://www.indiabix.com/_files/images/website/wrong.gif
D.
argv is a member of function pointer.
Answer: Option B
Learn more problems on : Complicated Declarations
Discuss about this problem : Discuss in Forum

18.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=-3, j=2, k=0, m;
    m = ++i && ++j || ++k;
    printf("%d, %d, %d, %d\n", i, j, k, m);
    return 0;
}
A.
1, 2, 0, 1
B.
-3, 2, 0, 1
http://www.indiabix.com/_files/images/website/wrong.gif
C.
-2, 3, 0, 1
D.
2, 3, 1, 1
Answer: Option C
Explanation:
Step 1: int i=-3, j=2, k=0, m; here variable i, j, k, m are declared as an integer type and variable i, j, k are initialized to -3, 2, 0 respectively.
Step 2: m = ++i && ++j || ++k;
becomes m = (-2 && 3) || ++k;
becomes m = TRUE || ++k;.
(++k) is not executed because (-2 && 3) alone return TRUE.
Hence this statement becomes TRUE. So it returns '1'(one). Hence m=1.
Step 3: printf("%d, %d, %d, %d\n", i, j, k, m); In the previous step the value of i,j are increemented by '1'(one).
Hence the output is "-2, 3, 0, 1".
Learn more problems on : Expressions
Discuss about this problem : Discuss in Forum

19.
By default structure variable will be of auto storage class
A.
Yes
B.
No
http://www.indiabix.com/_files/images/website/wrong.gif
Answer: Option A
Learn more problems on : Structures, Unions, Enums
Discuss about this problem : Discuss in Forum

20.
Can we write a function that takes a variable argument list and passes the list to another function?
A.
Yes
http://www.indiabix.com/_files/images/website/accept.png
B.
No
Answer: Option A
Learn more problems on : Variable Number of Arguments
Discuss about this problem : Discuss in Forum


No comments:

Post a Comment