VIVA QUESTIONS

image

image

Viva questions will be from all the topic of CSE202

THESE QUES WILL ALSO BE THERE

Que~1 : Difference between character array and string?

Que~2 : Use of file handeling?

Que~3 : Types of file?

Que~4 : Diff b/w Text and Binary files?

Que~5 : Storage Classes

WE ARE GETTING SOME ERROR IN SERVER WHILE UPLOADING PROGRAMS RELATED TO FILE HANDLING. KEEP WATCHING THIS AREA FOR PROGRAMS….

STACKS (USING TEMPLATES AND EXCEPTION H.)

We have posted a program on stacks operation which is done by normal class and function methood. Here is the generic function to make it more useful with different types of data. Go through Program ->

#include"conio.h"
#include"iostream"
using namespace std;
template<class t>
class stack
{
    private:
        t sta[50];
        int siz;
    public:
        stack()
        {
            siz=0;
        }
        t push();
        t pop();
        t display();
        
};
template<class t>
t stack<t>::push() // push fuction
{
    t num;
    if(siz==4)
    {
        cout<<"\nStack is already full.";
        return 0;
    }
    else
    {
        cout<<"\nEnter element : ";
        cin>>num;
        sta[siz]=num;
        siz++;
    }
    return 0;
}
template<class t>
t stack<t>::pop() //pop function;
{
    t num1;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else
    {
        num1=sta[siz-1];
        cout<<"\nElement popped : "<<num1;
        siz--;
    }
    return num1;
}
//c#ode studio appreciates this program;
template<class t>
t stack<t>::display() //display function;
{
    int i,j;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else 
    {
        for(i=siz-1,j=0;i>=0;i--,j++)
        {
            cout<<"\nELEMENT <"<<j<<"> IS : "<<sta[i];
        }
    }
}
int main()
{
    int choice=0;
    cout<<"\nNEED OPERATION WITH INTEGERS OR FLOAT( 1:INTEGER , 2:FLOAT ) : ";
    cin>>choice;
    try
    {
        if(choice==1)
        {
            stack <int> obj;
            int a;
            cout<<"\n_________STACK FUNCTIONS_________ "<<endl;
            int option=1;
            while(option)
            {
                cout<<"\n1:PUSH \n2:POP \n3:DISPLAY ";
                cout<<"\nEnter Choice : ";
                cin>>a;
                switch(a)
                {
                    case 1:
                        obj.push();
                        break;
                    case 2:
                        obj.pop();
                        break;
                    case 3:
                        obj.display();
                        break;
                    default:
                        cout<<"\nEnter correct choice... 404 ERROR <CHOICE NOT FOUND>";
                        return 0;
                }
                cout<<"\n1: Continue, 0:Exit :: ";
                cin>>option;
            }
        }
        else
            if(choice==2)
            {
                stack <float> obj;
                int a;
                cout<<"\n_________STACK FUNCTIONS_________ "<<endl;
                int option=1;
                while(option)
                {
                    cout<<"\n1:PUSH \n2:POP \n3:DISPLAY ";
                    cout<<"\nEnter Choice : ";
                    cin>>a;
                    switch(a)
                    {
                        case 1:
                            obj.push();
                            break;
                        case 2:
                            obj.pop();
                            break;
                        case 3:
                            obj.display();
                            break;
                        default:
                            cout<<"\nERROR 404-CHOICE DOESN'T EXISTS....TERMINATING PROGRAM";
                            return 0;
                    }
                    cout<<"\n1: Continue, 0:Exit :: ";
                    cin>>option;
                }
            }
            else
                throw choice;
    }
    catch(int x)
    {
        cout<<"\n\nERROR 404-CHOICE "<<x<<" DOESN'T EXISTS....TERMINATING PROGRAM";
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}


STACK_EXP

Bubble sort using Generic Function

Question : Implement the logic to arrange array in ascending order using generic function;

Answer :

#include"conio.h"
#include"iostream"
using namespace std;
template<typename data_type>
int sort(data_type *d,int size)
{
    int i,j;
    data_type a;
    for(i=1;i<size;i++)
        for(j=size-1;j>=i;j--)
        {
            if(d[j-1]>d[j])
            {
                a=d[j-1];
                d[j-1]=d[j];
                d[j]=a;
            }
        }
}
int main()
{
    int g[10];
    double h[10];
    int i;
    cout<<"\nInsert Integer Type No. : ";
    for(i=0;i<10;i++)
    {
        cout<<"\nEnter "<<i<<" No. :";
        cin>>g[i];
    }
    cout<<"\nInsert Float Type No. : ";
    for(i=0;i<10;i++)
    {
        cout<<"\nEnter "<<i<<" No. :";
        cin>>h[i];
    }
    sort(g,10);
    sort(h,10);
    cout<<"\nOutput after sorting integer values"<<endl;
    for(i=0;i<10;i++)
    {
        cout<<"\nInt "<<i+1<<" No. :";
        cout<<g[i];
    }
    cout<<"\nOutput after sorting floating values"<<endl;
    for(i=0;i<10;i++)
    {
        cout<<"\nFloat "<<i+1<<" No. :";
        cout<<h[i];
    }
    cout<<"\n\nProgramming @ C#ODE STUDIO";
    getch();
    return 0;
}

 

sort_temp1sort_temp2

Finding Roots Using Exception Handling

Question – WAP TO FIND THE ROOT OF AN EXPRESSION AS ” ax² + bx +c = 0″ ;

Answer –

#include"conio.h"
#include"iostream"
#include"math.h"
using namespace std;
int main()
{
    try
    {
        float a,b,c,d,root1,root2;
        cout<<"\nEnter 3 Coeff : "<<endl;
        cout<<"\nEnter A: ";
        cin>>a;
        cout<<"\nEnter B: ";
        cin>>b;
        cout<<"\nEnter C: ";
        cin>>c;
        if(a==0)
        {
            if(b==0)
            {
                throw b;
            }
            else
            {
                d=-c/b;
                cout<<"\n";
                cout<<"\nSolution will be :"<<d;
            }
        }
        else
        {
            d=b*b-4*a*c;
            if(d==0)
            {
                root1=(-b)/(2*a);
                root2=root1;
                cout<<"\nBoth the roots will be same : "<<root1<<" & "<<root2;
            }
            else
            {
                root1=(-b+sqrt(d))/(2*a);
                root2=(-b-sqrt(d))/(2*a);
                cout<<"\nRoot 1 : "<<root1<<"\t Root 2 : "<<root2;
            }
        }
    }
    catch(float x)
    {
        cout<<"\nBOTH THE VALUES CANT BE ZERO IN 'a*x^2 + b*x + c = 0 ";
    }
    cout<<"\n\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}

roots


					

Armstrong Number:

By: Vinay Kumar
College : LPU

Question : WAP to check whether number is armstrong number or not ;

Armstrong number= suppose 153 = cube(1) + cube (5) + cube(3) = 1+125+27 =153

#include"conio.h"
#include"iostream"
#include"math.h"
using namespace std;
int main()
{
    int a,b,sum=0,num;
    cout<<"\nEnter Number : ";
    cin>>num;
    a=num;
    while(a!=0)
    {
        b=a%10;
        sum+=b*b*b;
        a/=10;
    }
    if(sum==num)
    {
        cout<<"\nARMSTRONG NUMBER ";
    }
    else
    {
        cout<<"\nNOT AN ARMSTRONG NUMBER";
    }
    cout<<"\nPROGRAMMING @ C#ODE STUDIO";
    getch();
}

ARMS

Complex Number : Operations

By: Arun GuptaCollege : LPU

Question  : Perform addition, subtraction and multiplication operation on complex number using operator overloading ;

Answer:

#include"conio.h"
#include"iostream"
using namespace std;
class complex
{
    float real,img;
    public:
        void agn(float x, float y)
        {
            real=x;
            img=y;
        }
         void disp()
        {
            if(img>=0)
                cout<<real<<" + "<<img<<" i ";
            else
                cout<<real<<img<<" i ";
        }
        complex operator +(complex oj)
        {
            complex obj;
            obj.real=real+oj.real;
            obj.img=img+oj.img;
            return obj;
        }
        complex operator -(complex oj)
        {
            complex obj;
            obj.real=real - oj.real;
            obj.img=img - oj.img;
            return obj;
        }
        complex operator *(complex oj)
        {
            complex obj;
            obj.real=real*oj.real;
            obj.img=img*oj.img;
            return obj;
        }
};
int main()
{
    float x,y,a,b;
    int choice;
    complex obj1,obj2,obj3;
    cout<<"\nInput Value For 1st Complex No. :";
    cout<<"\nReal : ";
    cin>>x;
    cout<<"\nImaginary : ";
    cin>>y;
    cout<<"\nInput Value For 2nd Complex No. :";
    cout<<"\nReal : ";
    cin>>a;
    cout<<"\nImaginary : ";
    cin>>b;
    obj1.agn(x,y);
    obj2.agn(a,b);
    cout<<"\nValues Are : ";
    cout<<"\nFor 1st Complex No. :";
    obj1.disp();
    cout<<"\nFor 2nd Complex No. :";
    obj2.disp();
    cout<<"\n\nMenu : ";
    cout<<"\n\t1:ADDITION \n\t2:SUBTRACTION \n\t3:MULTIPLICATION \n\t0:EXIT";
    cout<<"\nMake Choice : ";
    cin>>choice;
    switch(choice)
    {
        case 0:
            cout<<"\nEXITING. . . .";
            break;
        case 1:
            obj3=obj1+obj2;
            cout<<"\nAnswer Will Be : ";
            obj3.disp();
            break;
        case 2:
            obj3=obj1-obj2;
            cout<<"\nAnswer Will Be : ";
            obj3.disp();
            break;
        case 3:
            obj3=obj1*obj2;
            cout<<"\nAnswer Will Be : ";
            obj3.disp();
            break;
        default:
            cout<<"\nWrong Choice has been made : Exiting . . .";
    }
    cout<<"\nProgramming @ c#ode studio";
    getch();
    return 0;
}

Addition :                                                                                    Multiplication:

complex complex2

 

Stack Operations;

Name: Rahul
Email: rahultiwari.uk@gmail.com
Question: Write a genetic class to implement stack with array as data member and implement push and pop operation with member function.
College: LPU

We can perform two operation with stack;

Push – To put an element at vacant position without violation of stack rule;
Pop- We can throw a element from a position which is at top most i.e. STACK RULE;

STACK

#include"conio.h"
#include"iostream"
using namespace std;
class stack
{
    private:
        int sta[50];
        int siz;
    public:
        stack()
        {
            siz=0;
        }
        int push();
        int pop();
        int display();

};
int stack::push() // push fuction
{
    int num;
    if(siz==4)
    {
        cout<<"\nStack is already full.";
        return 0;
    }
    else
    {
        cout<<"\nEnter element : ";
        cin>>num;
        sta[siz]=num;
        siz++;
    }
    return 0;
}
int stack::pop() //pop function;
{
    int num1;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else
    {
        num1=sta[siz-1];
        cout<<"\nElement popped : "<<num1;
        siz--;
    }
    return num1;
}
//c#ode studio appreciates this program;
int stack::display() //display function;
{
    int i,j;
    if(siz==0)
    {
        cout<<"\nStack is empty.";
        return 0;
    }
    else 
    {
        for(i=siz-1,j=0;i>=0;i--,j++)
        {
            cout<<"\nELEMENT <"<<j<<"> IS : "<<sta[i];
        }
    }
}
int main()
{
    stack obj;
    int a;
    cout<<"\n_________STACK FUNCTIONS_________ "<<endl;
    int option=1;
    while(option)
    {
        cout<<"\n1:PUSH \n2:POP \n3:DISPLAY ";
        cout<<"\nEnter Choice : ";
        cin>>a;
        switch(a)
        {
            case 1:
                obj.push();
                break;
            case 2:
                obj.pop();
                break;
            case 3:
                obj.display();
                break;
            default:
                cout<<"\nEnter correct choice... 404 ERROR <CHOICE NOT FOUND>";
                return 0;
        }
        cout<<"\n1: Continue, 0:Exit :: ";
        cin>>option;
    }
    cout<<"\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}

stack_push_pop

TYPE CASTING (CLASS TO CLASS)

Que- Wap to make a class’s member of another class by using type conversion;

Answer-

#include<iostream.h>
#include<conio.h>
class shop
{

int code;
int items;
float price;
public:
shop(int a,int b,int c)
{

code=a;
items=b;
price=c;

}
void display()
{

cout<<“\nCode  : “<<code;
cout<<“\nItems : “<<items;
cout<<“\nPrice : “<<price;

}
int getcode()
{

return code;

}

int getitem()
{

return items;

}

int getprice()

{
return price;

}

};
class detail
{

int code;
float value;

public:

detail()
{

code=0;
value=0;

}
void display()
{

cout<<“Code  : “<<code<<endl;
cout<<“Value : “<<value<<endl;

}
detail(shop p) //constructor for conversion
{

code=p.getcode();
value=p.getitem()*p.getprice();

}
};

int main()
{

shop s1(100,5,140);
detail d1;
char *c={“PROGRAMMING @ C#ODE STUDIO”};
d1=s1;  //Invoke Constructor in Invent2 for conversion
cout<<“\nProduct details – shop class”;
s1.display();
cout<<“\n\n\nProduct details – deetail class\n”;
d1.display();
cout<<“\n\n”<<c;
getch();

}

classtoclass

Predefined Mnipulators:

Following are the standard manipulators normally used in the stream classes:

•endl
•hex, dec, oct
•setbase
•setw
•setfill
•setprecision
•ends
•ws
•flush

(a) Endl-the endl is an output manipulator to generate a carriage return or line feed character. Theendl may be used several times in a C++ statement.

For example,

(1)

cout << “ a “ << endl << “b” << endl;

(b) Setbase()  The setbase() manipulator is used to convert the base of one numeric value into another base. Following are the common base converters in C++.

dec   – decimal base (base = 10)

hex   – hexadecimal base (base = 16)

(c)   Setw ()  

The setw ( ) stands for the set width. The setw () manipulator is used to specify the minimum number of character positions on the output field a variable will consume.

The general format of the setw manipulator function is

setw( int w )

Which changes the field width to w, but only for the next insertion. The default field width is 0.

For example,

cout << setw (1) << a << endl;

cout << setw (10) << a << endl;

(d)  Setfill()

The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value.

The general syntax of the setfill ( ) manipulator is

setfill( char f)

which changes the fill character to f. The default fill character is a space.

For example,

setfill ( ‘ . ’ ) ; / / fill a dot ( . ) character

setfill ( ‘ * ’ ) / / fill a asterisk (*) character

(e) Setprecision()

  The setprecision ( ) is used to control the number of digits of an output stream display of a floating point value. The setprecision ( ) manipulator prototype is defined in the header file <iomanip.h>.

The general syntax of the setprecision manipulator is

Setprecision (int p)

Which sets the precision for floating point insertions to p. The default precision is 6  eg:- 0.0000001

(f)  Ends

  The ends is a manipulator used to attach a null terminating character (‘’) at the end of a string. The ends manipulator takes no argument whenever it is invoked. This causes a null character to the output.

(g) Ws 

 The manipulator function ws stands for white space. It is used to ignore the leading white space that precedes the first field.

1.Noskipws

Skipws

(h)  Flush   The flush member function is used to cause the stream associated with the output to be completed emptied.

For input on the screen, this is not necessary as all output is flushed automatically. However, in the case of a disk file begin copied to another, it has to flush the output buffer prior to rewinding the output file for continued use. The function flush ( ) does not have anything to do with flushing the input buffer.

Operators :

Pos. Operator Description Method
1 :: scope Left-to-right
2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right
3 ++ -- ~ ! sizeof() new delete unary (prefix) Right-to-left
* & indirection and reference (pointers)
+ - unary sign operator
4 (type) type casting Right-to-left
5 .* ->* pointer-to-member Left-to-right
6 * / % multiplicative Left-to-right
7 + - additive Left-to-right
8 << >> shift Left-to-right
9 < > <= >= relational Left-to-right
10 == != equality Left-to-right
11 & bitwise AND Left-to-right
12 ^ bitwise XOR Left-to-right
13 | bitwise OR Left-to-right
14 && logical AND Left-to-right
15 || logical OR Left-to-right
16 ?: conditional Right-to-left
17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left
18 , comma Left-to-right