10 Objective type interview questions with answers on C/C++
Before you start:-
- You can check your answer by filling a,b,c,d or e in the text box.
- You can get the answer by clicking Give Up Button.
Try yourself before giving up 🙂
Q1) What will the below program prints ?
#include void e( int ); main( ) { int a; a=3; e(a); } void e(int n) { if (n>0) { e(--n); printf("%d",n); e(--n); } }
Options: a) 0120 b) 0121 c) 0201 d) 0211
#include main( ) { int a[5]= {1,2,3,4,5}; int*ptr = (int*)(&a+1); printf("%d %d", *(a+1), *(ptr-1)); }
Options: a)2 2 b) 2 1 c) 2 5 d) None
#include void foo(int [][3]); main( ) { int a[3][3]={ {1,2,3}, {4,5,6}, {7,8,9} }; foo(a); printf("%d", a[2][1]); } void foo(int b[][3]) { ++b; b[1][1]=35; }
Options: a)8, b) 9, c) 7, d)35
If performance is a critical requirement, then storage class should be used is ?
a)auto b) register c) extern d) static
Q5)
In a program
1-> char str1[ ] = “abc”;
2-> char *str2 = “xyz”;
3-> str2 = str1;
4-> str1 = str2;
Options:
a)compile error at line 3
b)compile error at line 4
c)no problem in the code
d)run time error
Q6)
int A = sizeof(char*) int B = sizeof(int*)
a)A=B b)A<B c)A>B d) depends on compiler
Q7)
void func (int y) { static int x; . . . }
x and y will be stored on respectively in ?
Options:
a) stack and heap b) stack and data section c) data section and stack d) heap and data section
Q8)
#include main(){ int i=5; ++i++; printf("%d", i); }
Options:
a)5 b) 6 c) 7 d) compilation error
Q9)
Statement 1-> const char *p1=”hello world”;
Statement 2-> char *const p2=”hello world”;
Statement 3-> p1=”welcome”;
Statement 4-> p2=”good bye”;
Statement 5-> *(p1+3) = ‘A’;
Statement 6-> *(p2+3) = ‘A’;
Which statement cause problem ?
Options: a)4 & 5 b) 1 & 3 c) 3 & 5 d) 2 & 3
Q10)
#include int function() { static int a=0; a=a+1; return a; } main() { function(); function(); printf("%d", function()); }
Options: a) 1 b) 2 c) 3 d) None
Mukesh Kumar says
main( )
{
int a[5]= {1,2,3,4,5};
int*ptr = (int*)(&a+1);
printf(“%d %d”, *(a+1), *(ptr-1));
}
This program gives output as :: 2, 5
2 is ok. But how 5 come.
What is happening in the line — int*ptr = (int*)(&a+1);
can any one please explain.
Thanks.