Q. What is the output of this program ?
#include <stdio.h>
int main(int argc, char* argv[])
{
int a = 10;
int *aPtr = &a; // Let &a = 0x98
char *bPtr = (char*)aPtr;
aPtr++;
bPtr++;
printf("%d %d", aPtr, bPtr);
return 0;
}
Sol: 156 153
aPtr being an int pointer advances by 4 bytes, whereas bPtr being char pointer advances by 1 byte.
Q. What is the output of the following C++ program ?
#include <iostream>
using namespace std;
class A{
public:
void f(){ cout << "A" << endl;}
};
class B{
public:
void f(){ cout << "B" << endl;}
};
class C : public A, public B {
};
int main(int argc, char* argv[])
{
C cObj;
cObj.f();
return 0;
}
Sol:
Compiler ERROR: Request for member 'f' is ambiguous.
Candidates are void B::f() and void A::f()
Q. Suppose an int occupies 4 bytes of memory and a char occupies 1 byte of memory. Then what will be the output of the following code ?
#include <stdio.h>
int main()
{
int a[] = {2,1,3,5,6,9};
char ch[] = {'a', 'b', 'm', 'x', 'e'};
printf("%d \t %d", (&a[3] - &a[0]), (&ch[3] - &ch[0]));
return 0;
}
Sol: 3 3
Q. Predict the output of the program.
main()
{
struct s1{ int i; };
struct s2{ int i; };
struct s1 st1;
struct s2 st2;
st1.i = 10;
st2 = st1;
printf("%d \n", st2.i);
return 0;
}
Sol: Error: cannot use assignment operator = with different struct types.
Q. What is the output of the following program ?
#include <stdio.h>
main()
{
int i,j;
int a[3][3] = {1,2,3,4,5,6,7,8,9};
for(i = 2; i >= 0; i--)
{
for(j = 2; j >=0; j--)
{
printf("%d ", *(*(mat+j)+i));
}
}
return 0;
}
Sol: 9 6 3 8 5 2 7 4 1
Q. Write a program to swap 2 variables without using a temporary variable.
Sol:
The main trick here is to use Bit manipulation.
Example:
Let int x = 15, y= 46
Then their Bit representation will be:
x : 00000000 00000000 00000000 00001111
y : 00000000 00000000 00000000 00101110
x = x XOR y : 00000000 00000000 00000000 00100001
y = x XOR y : 00000000 00000000 00000000 00001111 ---> 15
x = x XOR y : 00000000 00000000 00000000 00101110 ---> 46
Q. How many times is the printf statement executed in the following code sample for n=10?
fun(n);
void fun(int n)
{
int i;
for(i=0; i<=n; i++)
{
fun(n-i);
printf("Hello \n");
}
}
Sol: Zero times
The program will enter into an infinite recursion(loop), before printf is executed even once.
Q. What will be the output of the following program ?
int main()
{
struct employee
{
char emp[];
int empno;
float sal;
};
struct employee em = {"CODING TONIC"};
printf("%d, %f \n", em.empno, em.sal);
printf("%s", em.emp);
return 0;
}
Sol: Error. In struct variable emp[], array size should be specified.
If the size is specified, like char emp[24]; then the output will be:
0, 0.000000 CODING TONIC
On the other hand, if we initialize the em variable as(without specifying size of emp[])
struct employee em = {0};
Then, it will not give error.The output will be:
0, 0.000000
Q.What will be the output of the following code ?
#define infiniteloop while(1)
main()
{
infiniteloop;
printf("CodingTonic \n");
return 0;
}
Sol: Nothing
infiniteloop in main ends with ; . So, the loop will never terminate, and CodingTonic will not be printed.
Q. Predict the output:
main()
{
int a = 2;
int b = 3;
printf("%d", a+++b);
}
Sol: 5
It will be evaluated as (a++) + b
Q. To reduce the bureaucratic red-tape and also provide better growth opportunities for employees, a government department wants to cut down on reporting hierarchy. They implement a rule which says, all employees who have a single direct reportee will be moved to a new department. Their reportee will be asked to report to their manager, thus cutting the hierarchy. This rule will take effect across all reporting levels.
Given a reportee-manager relationship like below, can you
(a) Write an algorithm which runs in linear time to implement this change.
(b)Identify persons who will be moved to the new department.
(c) Give the new reportee-manager relationship (in a tabular form, similar to below) after the change has been effected.
Reportee Manager
Indhu Rani
Ashwin Raj
Ram Anitha
Radhika Anitha
Mumtaz Indhu
Raj Abhi
Rani Abhi
Ben Ashwin
Sanjana Ram
Aditi Radhika
Abhay Ashwin
Anitha Abhi
Sol:
The above table can be written in tree form as:
[Abhi]
/ | \
/ | \
Rani Anitha Raj
/ / \ \
/ / \ \
Indhu Ram Radhika Ashwin
| | | / \
| | | / \
Mumtaz Sanjana Aditi Ben Abhay
Algorithm:
(1.) Create a node structure having parent pointer and children's list.
(2.) For every node starting from root node, check if it's children count = 1.
(3.) If the children count is 1 and its parent is not null, then remove its child, append the child to its parent, and append the current node to root node's children list.
Nicely organized. Keep the good work. Please share interviews of more companies.
ReplyDelete