Showing posts with label interview. Show all posts
Showing posts with label interview. Show all posts

Monday, February 18, 2013

Interview question on Virtual Function

Que. Can a methode in base class which is declared as virtual be called  using the base class pointer which is pointing to a derived  class object?
 


#include <iostream>
using namespace std;

class B
{
    public:
        virtual void foo()
        {
            cout << "\n its from B" << endl;
        }
};

class D: public B
{
    public:
         void foo()
        {
            cout << "\n its from D" << endl;
        }
};

int main()
{
    B * b = new D();
    b->B::foo();
    b->foo();   //here parent class access the function of child class
//  b->D::foo();  //error -->'D' is not a base of 'B'
    delete b;
    b = 0;

//    D * d = new B;  //error --> invalid conversion from 'B*' to 'D*'
    return 0;
}


OUTPUT:

See in C# also CLICK HERE

Wednesday, February 6, 2013

Understand unary and arithmetic operator

#include <stdio.h>

int main()
{
    int x = 3 + 4 % 5 - 6;
    int q = -3 * 4 % - 6 / 5;
   
    printf ("\n %d %d \n",x,q);
   
    int r,t,y,u,i,o,p,l,k;
    r = -5 % -5;    //0
    t = -5 % 5;    //0
    y = 5 % -5;     //0
   
    u = -3 % -5;   //-3
    i = -3 % 5;    //-3
    o = 3 % -5;   //3
   
    p = -5 % -3 ;   //-2
    l = -5 % 3;     //-2
    k = 5 % -3;    //2
   
    int p1,l1,k1;
    p1 = -5 / -3 ; //1      [-(5/3)common aise nahi hoga,ye katega]  
    l1 = -5 / 3;   //-1     [-4 + -2 --> -(4+2) here is allowed]
    k1 = 5 / -3;   //-1
   
    printf("\n %d %d %d   %d %d %d    %d %d %d    %d %d %d \n",r,t,y,u,i,o,p,l,k,p1,l1,k1);
   
    return 0;
}


OUTOUT:

For more info click here

Monday, February 4, 2013

Understand the ++ and -- operator

//Please don't copy-paste this program ; make your own implementation
#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    int a;
    a = 5;
    cout << ++a << endl; // pre-increment -- 6
    cout << --a << endl; // pre-decrement -- 4
    cout << a++ << endl; // post-incremenr -- 5
    cout << a-- << endl; // post-decrement -- 5
   
    cout << a++ << a++ << endl; // 5 -(+1)-> 6
   
    cout << a++ + a++ + a++ << endl ; // 5 -(+1)-> 6 -(+1)-> 7 = 18
   
    //This goes left-to-right
    cout << a++ + a++ + --a; // 5 -(+1)-> 6 -(+1)-> 7 (-1) -->6 =17
   
    // it goes right-to-left
    cout << a++ , a++ , a++;  //7, <-(+1)- 6, <-(+1)- 5
   
    //it goes 1st right-to-left and then left-to-right
    cout << a++ + a++ + a--, ++a; // 21,6
   
    printf ( "%d %d %d %d %d\n",x++, x--, ++x, --x, x++);  // 5 6 6 5 5
   
    //For Practice

    int b = 5, c = 5, i, j, j1;
    i = a++ + a++ + a++;               
    j = ++b + ++b + ++b;
    j1 = c++ + c++ + c--;
   
    printf ("\n value of i using printf:%d\n", i);
    cout << "\n Value of  i : " << i << endl
          << "\n Value of  j : " << j << endl
          << "\n Value of j1 : " << j1 << endl;
       
    printf("value of 1st print is: %d\n", 59);
    printf("value of 2nd print is: %c\n\n", 59);
}

-----------------------------------------------------------------------------------------------

//What is the output of this program
#include <stdio.h>
int main()
{
    int x=20,y=35;

    x=x++ + y++;

    y=++x + ++y;

    printf ( "%d, %d", x,y);
    return 0;
}

The output for your program is 57, 94.

Explanation:
x = 20, y = 35

x = x++ + y++ = 20 + 35 (x and y would be incremented after the addition) = 55
x++ -----> x = 55 + 1 = 56
y++ -----> y = 35 + 1 = 36

++x -----> x = 56 + 1 = 57 (x and y would be incremented
++y -----> y = 36 + 1 = 37 before the addition)
y = ++x + ++y = 57 + 37 = 94

Hence final values of x and y are 57 and 94 respectively. 


Understand this way:
d=x++; //the value of x is stored to d and d is increment by 1.x remains the same.d=20 x=21
printf ("%d %d",d,x);   // 20 21

x = x++;     
printf("%d",x);    //21

d = x++ + 1; 
x = x++ + 1;   
printf("%d %d");     //d=21 x=22
 

d=++x; //here increment is done first after that value is stored=x=21


For further detail click here.

Monday, January 28, 2013

Program to point some thing without using semicolon

#include <stdio.h>

int main()
{

    if (printf("hello world\n"))
    {
        //do nothing
    }
   
}


OUTPUT:

Monday, January 21, 2013

Write a program to swap 2 numbers using bitwise operators

//Write a program to swap 2 number without arithmetic operator

#include <stdio.h>

int main()
{
    int i=65;
    int k=120;
   
    printf("\n value of i=%d k=%d before swapping",i,k);
    i = i^k;
    k = i^k;
    i = i^k;
   
    printf("\n value of i=%d k=%d after swapping",i,k);
    return 0;
}


OUTPUT:

Tuesday, January 1, 2013

What is threading

What is a thread:
  • A thread is the entity with in a process that can be scheduled for execution.
  • Threads are much like lightweight processes.
  • Allow the program to execute sequential actions or many actions at once.
  • All the threads in a process share the same address space.
  • It's a small independent chunk of code that can run concurrently with other threads.
  • Each thread runs independently of the others.
  • Each thread may run a different sequence of instructions.
  • A thread isn't a whole program.
  • Each process has atleast one thread.
Note: It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process.

What is a process:
  • An executing instance of a program is called a process.
  • Program under execution is known as process.
  • A process is always stored in the main memory also termed as the primary memory or random access memory.
  • Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
  • Each process provides the resources needed to execute a program.
  • Several process may be associated with a same program.
  • A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution.
  • Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

What is the difference between Process and Thread :
  1. Threads are easier to create than processes since they don't require a separate address space.  
  2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.
  3. Threads are considered lightweight because they use far less resources than processes.
  4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other. This is really another way of stating 2 above.
  5. A process can consist of multiple threads.
  6. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  7. Both threads and processes are atomic units of OS resource allocation.

What is Threading (or multithreading):
  • Multithreading is the ability of a program or process to manage multiple requesets by the same user at same time without having to have multiple copies of the program (that is running).
  • If a program have multiple thread that kind of program known as multithreading program.
  • Threads allows for multithreading.

Advantage of threading:
There are 2 main reasons to use threading in an application.
  • Multithreading improve application performance.
  • Separation of concerns.

When not to use multithreading:
  • The only reason not to use threading is when the benefit is not worth the cost.
  • The performance gain might not be as large as expected.

Disadvantage of threading:
  • It can complicate the code, making it harder to understand and more prone to bugs. Therefore it’s only worth doing for those performance-critical parts of the application where there’s the potential for measurable gain.

Why we use Threading:
  • In simple, to achieve concurrency.

What is concurrency:
  • Concurrency is about two or more separate activities happening at the same time.
  • In term of computers, a single system performing multiple independent activities in parallel, rather than sequentially, or one after the other.
  • Concurrency meaning in plain English -->Acting together, as agents, circumstances or events.

Why use concurrency:
The almost same reason that for threading.
  • Separation of concerns.
  • Performance.
           
Explaination for separation of concerns:
Separation of concerns is almost always a good idea when writing software; by grouping related bits of code together and keeping unrelated bits of code apart, you can make your programs easier to understand and test, and thus less likely to contain bugs. You can use concurrency to separate distinct areas of functionality, even when the operations in these distinct areas need to happen at the same time; without the explicit use of concurrency you either have to write a task-switching framework or actively make calls to unrelated areas of code during an operation.
Example:
A DVD player application for a desktop computer. Such an application fundamentally has two sets of responsibilities: not only does it have to read the data from the disk, decode the images and sound, and send them to the graphics and sound hardware in a timely fashion so the DVD plays without glitches, but it must also take input from the user, such as when the user clicks Pause or Return To Menu, or even Quit. In a single thread, the application has to check for user input at regular intervals during the playback, thus conflating the DVD playback code with the user interface code.

c++11 standard have the support of multithreaded programs.
c++11 standard provide components in the library for writing multithreaded application and it is known as 'thread'.
we achieve threading before c++11 standard.
what's the difference made by thread library in c++11 standard. (or why use thread library)
Well answer is: This will make it possible to write multithreaded c++ programs without relying on plateform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior.

What is Mutexes: (mighty)
  • Mutex stand for mutual exclusion and is the simple and most useful synchronization structure.
  • In essence it is a boolean that determines if an object is locked from use or not.

What is Semaphores: (super)
  • Semaphores are objects that allow a certain number of threads to access a certain object before it begins blocking it.
  • A semaphore with a value 2 will allow the first 2 threads in the queue access to it then block any threads after that.
  • A mutex is in essence just a semaphore that allows one thread at a time to access an object.

Saturday, December 29, 2012

why use interfaces

So if an interface implements no functionality then why should we use them?
     Using interface based design concept provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface. Interfaces add a plug and play like architecture into your applications. Interfaces help define a contract (agreement or blueprint, however you chose to define it), between your application and other objects. This indicates what sort of methods, properties and events are exposed by an object.


This article not complete yet, we are in developing  phase, we are sorry

Wednesday, October 17, 2012

What is the retrun-type of printf

About printf:    
printf is the formatted print routine that comes as part of the standard C library. the first argument to print is a format string. it describes how any remaining argument are to be printed. The charater % begins a print specification for an argument. In our program, %d told printf to interpret and print the next argument as a decimal number. printf can also output literal characters. In our program, we "printed" a newline character by giving its name (\n) in the format string. 
-----------------------------------------------------------------------------------------------
#include <stdio.h>

int main()
{
    int a = 57;
    printf ("\n");
   
    //What is the output of this

    printf ("%d ");    // This gives garbage value
    printf ("\n\n");    //this is used only to get clear output
   
    //What is the output of this
    printf ("%d",printf("%d",56));  //without space
    printf ("\n\n");
   
    //What is the output of this

    printf ("%d",printf("%d ",56));
    printf ("\n\n");
   
    //What is the output of this
    printf ("%d",printf("%d ",a));
    printf ("\n\n");
   
    //What is the output of this
    printf("%d ",printf("%d ",printf("%d ",56))); //that's very interesting
    printf ("\n\n");
   
    //What is the output of this
    printf ("%d \t %d ",58, a);               // 'space' -->takes 1 character
    printf ("\n\n");
   
    //What is the output of this
    printf("%d",printf ("%d \t %d ",58, a));  // \t -->takes 1 character
    printf ("\n\n");
   
    //What is the output of this
    printf("%d",printf ("%d\n\t %d ",a,59));  // \n -->takes 1 character
    printf ("\n");
   
    return 0;
}


OUTPUT:

Thursday, September 20, 2012

How to count the number of objects created of a class

#include <iostream>
using namespace std;

class Demo
{
    public:
        static int totalObject;
       
        Demo()
        {
            totalObject++;
        }
};

int Demo::totalObject(0);    //static variable initialized to 0

int main()
{
    Demo d1;
    Demo d2;
    Demo d3;
    Demo d4;
   
    cout << "\n Total no of object of Demo class is: " << Demo::totalObject << endl;
    return 0;
}


OUTPUT:

Tuesday, July 24, 2012

program to Remove duplicate character from a string


//this program uses the function of string library

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str1 = "silicon ABC ADC BCA";
    string str2;
    int pos;


    for(int i = 0; i < str1.length(); i++)
    {
        if( (pos = str2.find(str1[i])) < 0)
            str2 += str1[i];
    }


    cout << str2 << endl;
    return 0;
}


OUPUT: