How to swap two integer variables without using the third variable ?
See Solution: Swap two integer variables
How to measure 4 gallon of water
How to measure exactly 4 gallon of water from 3 gallon and 5 gallon jars, Given, you have unlimited water supply from a running tap.
It was also solved by Bruce Willis in one of his Die Hard movie to stop a bomb.
See Solution: How to measure 4 gallon water from 3 gallon and 5 gallon jarObjective Questions set C/C++ – Part 5
4 Objective type interview questions on C/C++ with answers
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)
#include int main(){ char a[5]="hello"; printf("%s",a); }
Options: a) will print “hello” only b) will print some garbage values c) will print “hello” atleast d) None
Objective Questions set C/C++ – Part 4
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)
#include main() { unsigned char i; int sum=0; for(i=0;i<300;i++) sum=sum+i; printf("%d",sum); }
Options: a) 44850 b) 45150 c) 300 d) Infinite Loop
Q2)
#include fun(int i) { int j=0; while (i & i-1) { j++; } printf("%d",j); } main(){ f(100); }
Options: a. 100 b. 99 c. 2 d. 3
Q3)
Code 1 : for(i=0; i<1000; i++) for(j=0; j<100; j++) x = y; Code 2 : for(i=0; i<100; i++) for(j=0; j<1000; j++) x = y;
Which code will execute faster ?
[Read more…]
Objective Questions set C/C++ – Part 3
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
Objective Questions set C/C++ – Part 2
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)
#include struct XX{ int a:10; int b:6; char c; } structure; int main(){ int i = sizeof(structure); printf("%d", i); }
Options: a) 4 b) 8 c) 9 d) 3 e) Compilation error
Q2)
#include #include main() { char *s; s="hot java"; strcpy(s,"solaris java"); printf("%s", s); }
Options:a) hot java b) solaris java c) compilation error d) Segmentation fault
Objective Questions set C/C++ – Part 1
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 be the output of the program below
#include main(){ int num[] = {1,4,8,12,16}; int *p,*q; int i; p = num; q = num+2; i = *p++; printf("%d, %d, %d\n",i, *p, *q); }
Options a) 4, 4, 8 b) 1, 4, 8 c) 2, 1, 8 d) 2, 4, 8
#include main(){ char *a[4]={"jaya","mahesh","chandra","swapant"}; int i = sizeof(a)/sizeof(char *); printf("i = %d\n", i); }
Options: a) 4 b) 7 c) 8 d) 1
Red and blue balls
You have three bags and three labels. One bag has only red balls, one has only blue balls and one has both red and blue balls. Three labels are R, B and RB. R label was meant for the bag with only red balls, B label was meant for the bag with only blue balls and RB for the bag with both red and blue balls. Ram by mistake labelled the bags wrongly such that all the labels are wrong, how many minimum number of balls he should pick and from what bags to correct the labels? It is given that each bag has unlimited number of balls to be picked.
See Solution : Red and blue ballsRiver crossing – the harder one
Now the difficulties are : if the dog is left with anyone and the maid isn’t there to control him, he’ll bite. The dad can’t be left with any of the daughters when the mom isn’t there. Likewise, the mom can’t be trusted alone with either of the sons when the dad isn’t there.
Remember! only an adult can operate the boat, and the boat can’t drive itself.See Solution : River crossing - the harder one
Defective coins
We have 10 bags of 1 Rupee coins. One bag contains all the defective coins, the weight of each coin in that bag is 1 gram lesser than the weight of a normal 1 Rupee coin. You have a spring balance, which tells the exact weight. After how many minimum no. of weighs you can separate the bag with defective coins.
Other variant of the same problem:
There are 10 machines in a factory. Each produces coins weighing 10 grams each. One day the factory owner cones to know that one of the machine is not functioning properly and produces coins of weight 9 grams. You have to find out the faulted machine. You ONLY have a weighing machine and you can use it only ONCE.