15.10.08

General C++ Interview questions

I have categorized Interview questions in three categories

- Basic:

- Advanced:

- STL:

BASIC C++ Interview questions:


Under OOPS concept:


- What is Class

- What is Object

- What are the different access specifiers in class.

- What is abstraction

- what is encapsulation

- what is Polymorphism

- compile time polymorphism

- run time polymorphism

- virtual function

- what is inheritance and why

Language Specific:


what is the difference between new and malloc

difference between free and delete

when to use delete and when to use free

what is operator overloading

what is friend function

Constructor and Destructor

what is reference and how it is different from pointer.

exception handling

try, throw and catch.

this pointer.

why friend function requires two arguments in binary operator overloading?


what is late binding.

what is the difference between inline functions and macros?

what is namespace

static data member initialization

overloading and overriding?

default arguments usage in functions

what is name mangling?



Advanced:


what is the size of empty class and why

what is Vtable and layout of Vtable

What is the use mutable?

what is copy constructor?

what is the use of const?

what is const correctness?

static_cast, dynamic_cast and reinterpret_cast.

what is pure virtual function?

what is abstract class?

What is the use of Using clause?

Code examples on Virtual functions

copy constructor assignment operator?

deep copying and shallow copying?

how to use c-style functions in C++ / usage of extern

How to start sshd service in debug mode.

- stop sshd service.
use /etc/init.d/sshd stop or svcadm disable ssh
- start the service by
/usr/lib/ssh/sshd -d -d -d

1.10.08

solaris packaging tools.

Package check tool "pkgchk"

I have come across a problem, that is, how to know that a particular "file" or "binary" or "daemon" belongs which package?

Let say we want know the binary "sendmail" belongs to which package?

The command is:
#pkgchk -l -p /usr/lib/sendmail

Pathname: /usr/lib/sendmail
Type: regular file
Expected mode: 2555
Expected owner: root
Expected group: smmsp
Expected file size (bytes): 1020544
Expected sum(1) of contents: 42649
Expected last modification: Aug 16 10:45:47 PM 2007
Referenced by the following packages:
SUNWsndmu
Current status: installed



23.7.08

How to strat and stop the some service in solaris

How to start and stop the services in solaris-10.

previous to solaris 10 we used to control the services using /etc/.init.d/ but soalris 10 provide svcadm to enable or disable service.

svcadm
Example to enable ftp serivce

#svcadm enable ftp

Check whether "ftp" service enable or not by using

#svcs -a | grep ftp
online 18:41:06 svc:/network/ftp:default

know your terminal

How to know through which terminal we connected to particular machine or present terminal type.

we have "who -a" commands which list the all the users presently logged in system. but how you will come to know with which terminal presently we connected.

let say the "who -a" output is:

. system boot Jun 30 19:51
. run-level 3 Jun 30 19:51 3 0 S
rc2 . Jun 30 19:54 old 89 id= s2 term=0 exit=0
rc3 . Jun 30 19:54 old 1750 id= s3 term=0 exit=0
LOGIN console Jun 30 19:54 1:53 2225 id= dt term=15 exit=0 (:0)
sac . Jun 30 19:54 old 2443 id= sc
root + console Jun 30 20:40 1:53 2444
sctpd . Jun 30 19:54 old 2447 id= US
start_DF + null Jun 30 19:54 . 2448
sufd_rc_ . Jun 30 19:54 old 2449 id=SUFD
zsmon . Jun 30 19:54 old 2503
.telnet /dev/pts/3 Jul 15 14:24 2349 id=t100 term=0 exit=0
root + pts/3 Jul 22 16:27 . 578 (10.143.64.25)
root pts/2 Jul 22 18:46 old 20139 id=ts/2 term=0 exit=0
.telnet /dev/pts/4 Jul 15 14:21 29587 id=tt/2 term=0 exit=0
.telnet /dev/pts/6 Jul 15 14:21 29594 id=tu/2 term=0 exit=0
root pts/4 Jul 22 18:46 old 5089 id=ts/4 term=0 exit=0

to know with which terminal we connected
use "tty" commnad.

#tty
/dev/pts/3



Use TTY commnad to know on wich terminal u are working now or what is ur terminal.

shared library comes with which package

How to know to which package the specified shared library belongs.

Sometimes it's required to know with which package particular shared library came.
There is a way to find this in solaris.

grep <> "/var/sadm/install/contents"


example to know with which package "libc.so" came

#grep libc.so /var/sadm/install/contents

Recursive grep in solaris

In solaris "grep" tool don't have option to grep the specified "pattern" recursively(not like linux) under current directory.
Generally "grep" tool grep the pattern in current directory only.

Workaround for this is:
Have to use "find" command to get the all the files and "piped" this data to grep command.
#find . -type f | xargs grep "pattern"

How to add User in Soalris ( Unix)

Adding user in solaris

useradd -d -m -s username.

Here adding user name called "raj", having home directory "/export/home/raj" and shell as "sh"
# useradd -d /export/home/raj -m -s /bin/sh raj

26.6.08

Swapping two integers without temporary variable.

We can do this thing in many ways, here are some.


int main()
{
int x, y;
printf("Enter x and y values \n");
scanf("%d %d", &x, &y);
printf("X=%d\t Y=%d\n", x, y);
printf("AFTER SWAP \n");

/* Swap using addition and subtraction */
x = x+y;/*Limitation is there addition should */
y = x-y;/* not over flow */
x = x-y;
printf("X=%d\t Y=%d\n", x, y);

/* Swap using multiplication and division */
x = x*y;/* Limitation is, Y should not be zero and*/
y = x/y;/* and x*y should not overflow */
x = x/y;
printf("X=%d\t Y=%d\n", x, y);

/* swap using exclusive OR */
x = x^y;
y = x^y;
x = x^y;
printf("X=%d\t Y=%d\n", x, y);

return 0;
}
Give the one line code to tell the given number is power of '2' or not.

The "C" code snippet is here:
  int main()
{
int num;

printf("Enter a number other than Zero \n");
scanf("%d", &num);

num&(num-1)?printf("Not a Power of 2 \n"):printf("Power of 2 \n");

return 0;
}


ld.so.1: fatal: .so: open failed: No such file or directory



This problem comes when the required ( liked *.so) libraries were not exist in the proper location in the system or the libraries not exist in system at all.

Remedy:

To know exactly which libraries are missing:

execute this command.

ldd "binaryname"

This command lists all the *.so that binary contains and there location in the system.

If any of the binary is missing it says .

From this report just copy the missing *.so into /usr/lib/ ( by default) or specified lib directory.

31.3.08

many people thought main would be the first function to be called.
But there are some exceptions where other function get called
before main actually called.


class mine {
public:
mine()
{
cout<<"Constructor called \n";
}
};

mine obj1;

int main() {
cout<<"we are in main";

return 0;
}


after running this first constructor will be called later main executed.

7.3.08

passing arguments

Passing arguments to function.

Let say
int fun ( int ) ;
is the function prototype.

Now function call was made like this.

fun(x++);

This is not good practice.
In the above call our intension is to pass the incremented value of 'x',
but it pass the value of x not x+1.The best practice is, fun(++x).

Code:

int main()
{
void test_fun(int);
int num=10;
test_fun(num++);
return 0;
}
void test_fun(int num)
{
printf("NUM=%d", num);
}

Output is : NUM=10

print 1 to 100 without using loop

Printing from 0 to 100 without using
any loop.

we can do this using recursion.

Code:
void print()
{

static int x;
if (100 == x )
return; /* or use exit call*/

printf("%d", x++);
print(x);

}

28.2.08

Swap the two numbers

This is the most frequently asked C question.

How to swap two numbers without using temporary variable.

There are many ways to do that, here are some.

Code snippets:
1)

int x, y; /* are the two numbers */
x = x+y;
y = x - y;
x = x - y;

2)
x = x*y;
y = x/y;
x = x/y;
3)
x = x ^ y;
y = x ^ y ;
x = x ^ y ;
NOTE: These logics have some limitations
Logic 1 has the Overflow problem when we add (x+y).
Logic 2 has the Overflow problem when we multiply ( x*y) and y should not be Zero.
Logic 3 has the same and its not works on the float numbers.

27.2.08

Even/Odd number

This is also one of the C Interview questions.

How to tell the given number is even or odd number in single line C code.

Code snippet:
int num;
num&1 ? printf("Odd Number\n") : printf("Even number\n");

Logic is every odd number will have least significant bit has 1
in binary representation of the number.

So when we do bitwise AND with number 1, obviously the
result is '1' for odd numbers and '0' for even numbers.

power of 2

This is one tricky bit usually asked in C interviews

How to tell the given number is Power of 2 or not in a one line 'C 'code.

There are many ways to do this, here is my logic.

Code snippet:

int num; /* holds the integer value */

num & (num-1) ? printf("Not Power of 2\n") : printf("Power of 2\n");


Boolean type

C99 supports _Bool keyword, which declares a two valued ( '0' or ' 1 ') integer.

Code snippet:

_Bool x; /* x will contain either ‘0’ or ‘1’.*/

x = 0;


Another way

include the header stdbool.h , it provides bool same as _Bool ( typedef ) and

two macros true and fase.

Code snippet:

#incldue "stdbool.h"

bool flag;

flag = true; /* flag value is true which is 1 */

flag = false; /*flag value false which is 0 */


Variable length Arrays/Dynamic Arrays

C99 added the feature called dynamic arrays in C.

Means we can specify the length of the array in the run time.

But there is one limitation that once the length of the array specified,

it’s not possible to change the length of array again.


Code fragment.

int length;

int arr[length];

Printf(“Enter the Length of the array \n”);

Scanf (“%d”, &length);



NOTE: Here the length of the arr is length and it should not be changed.

22.2.08

Array Initialization

Here i am discussing how many ways we can initialize the array in
the 'C' language

Normal initialization
int arr[10] = { 0, 1, 2, 3 , 4, 5, 6, 7, 8, 9 };

This is very normal way of initialization of the array.

-->Let say there is a situation that all of the array elements holds the same
element.Different ways to initialize that is

int arr[ 10] = {0,0,0,0,0,0,0,0,0,0,}; /* this is not a good way */
int arr[10] = {0}; /* Good Practice */

--> let say there is another situation where an array of length "10"
which contains some elements up to index 4 ( means 5 elements)
after that the array contains only zeros.

int arr[10] = {1,12,3,4,5,0,0,0,0,0}; /* This is not good practice */
int arr[10] = { 1,12,3,4,5}; /* rest filled with zeros. */

--> Let say there is another strange situation where first 3 elements in array
holds same value and next 2 elements hold the same value.

int arr[10] = {4,4,4,7,7,1,2,3,4,5}; /* This is normal initialization */
int arr[10] = {[0 ... 2]=4,[3 ... 4]=7,1,2,3,4,5}; /* This is good practice */

--> Let say there is another situation where 3rd element holds 90 ,
8th element holds 100 and rest all holds the zeros.

int arr[10] = {0,0,90,0,0,0,0,100,0,0};/* General practice */
int arr[10] = {[2]=90, [7]=100};/* Good practice */