You are on page 1of 21

C/C++ Based Test: 27 question 35 Marks

Q1 Find the output assume size of int is 4 byte

class base {

int arr[10];

};

class b1: public base { };

class b2: public base { };

class derived: public b1, public b2 {};

int main(void)

cout << sizeof(derived);

return 0;

A. 20
B. 40
C. 80
D. 120
E. None of these
Answer: (B)

Q2

1. What is the output of the given program?


int main(){
if(0){
cout<<"Hi";}
else{
cout<<"Bye";}
return 0;}
a) Hi
b) Bye
c) HiBye
d) Compilation Error
Answer: (B)

2. What is the output of the given program


class base{
public:
base(){
cout<<"BCon";}
~base(){
cout<<"BDest ";
}
};
class derived: public base
{
public:
derived()
{ cout<<"DCon ";
}
~derived()
{ cout<<"DDest ";
}
};

int main()
{
derived object;
return 0;
}
a) DconDDest
b) DconDDestBConBDest
c) BConDConDDestBDest
d) BConDConBDesDDest
Answer: (c)

3. What is the output of the following program


#include <iostream>

class Singleton
{
private:
static Singleton* instance;
Singleton();

public:
static Singleton* getInstance();
};

Singleton* Singleton::instance = 0;

Singleton* Singleton::getInstance()
{
if (instance == 0)
{
instance = new Singleton();
}

return instance;
}
Singleton::Singleton()
{}

int main()
{
Singleton* one = Singleton::getInstance();
Singleton* two = Singleton::getInstance();

if(one == two)
std::cout<<"Both pointers hold same object";
else
std::cout<<"They don't hold same object";

return 0;
}
a) Both pointers hold same object
b) They don't hold same object
c) Compilation Error
d) Exception occurred

Answer: a) Both pointers hold same object

4. Does the below code compiles successfully on C++?


class C
{
int a=7;

public:
C();
};
a) Yes
b) No
Answer: (A)

5. Which keyword in C++ 11 is used to prevent a virtual function from


being overridden?
a) Final
b) Static
c) Stop
d) Friend
Answer: (A)

6. Is below program of C++ 11 valid & runs without compilation error?

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

void print_square(int i)
{
cout<<i*i<<endl;
}
int main()
{
vector<int> v;
// vector gets filled
for_each(v.begin(), v.end(), print_square);
return 0;
}
a) Yes
b) No
c) Depends on Compiler
Answer: (A)

7. Is this valid C++ 11 program & get compiled without any error?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
vector<int> v;
// vector gets filled

for_each(v.begin(), v.end(),
[](int i) { cout<<i*i<<endl; } );

return 0;
}
a) Yes
b) No
Answer: (A)

8. If you use either Telnet or FTP, which is the highest layer you are using
to transmit data?
1. Application
2. Presentation
3. Session
4. Transport
Answer: (A)
9. Pushing an element into stack already having five elements and stack
size of 5, then stack becomes
a) Overflow
b) Crash
c) Underflow
d) User flow
Answer: (A)

10.
What is the value of the postfix expression 6 3 2 4 + – *:
a) 1
b) 40
c) 74
d) -18
Answer: (A)

11.

What will be the output of the following pseudocode?


Integer n
for (n = 3; n != 0; n--)
Print n
n = n-1
end for

a) 3 1
b) 3 2 1
c) 3
d) Infinite Loop
Answer: (D) Infinite Loop

12

How many times the following loop be executed?


{
ch = b ;
while(ch>= a && ch <= z)
ch++;
}

a) 0
b) 25
c) 26
d) 1
Answer: (B)

13.

#include <iostream>
using namespace std;
int a = 2;
int fun(int *a) {
::a *= *a;
cout<< ::a <<endl;
return *a; }
int main() {
int a = 9;
int&x = ::a;
::a += fun(&x);
cout<< x; }

Output:
4

14.

#include <iostream>
using namespace std;
int main() {
char *A[] = { "abcx", "dbba", "cccc"};
char var = *(A+1) - *A+1;
cout<< (*A + var); }
Output: bba

15. #include <iostream>


usingstd::cout;
class Test
{
public:
Test();
~Test();
};
Test::Test()
{
cout<< "Constructor is executed\n";
}
Test::~Test()
{
cout<< "Destructor is executed\n";
}
int main()
{
delete new Test();
return 0;
}

Output:
Constructor is executed

Destructor is executed

16.

#include <iostream>

using namespace std;

class X {
private:
static const int a = 76;
public:
static int getA() { return a; } };
int main() {
cout<<X::getA()<<endl;
return 0; }
Output:  76

17. #include<stdlib.h>
#include<iostream>
using namespace std;
class Test {
public:
void* operator new(size_t size);
void operator delete(void*);
Test() { cout<<"\n Constructor called"; }
~Test() { cout<<"\n Destructor called"; }
};
void* Test::operator new(size_t size)
{
cout<<"\n new called";
void *storage = malloc(size);
return storage;
}
void Test::operator delete(void *p )
{
cout<<"\n delete called";
free(p);
}
int main()
{
Test *m = new Test();
delete m;
return 0;
}
Output:  new called

Constructor called

Destructor called

delete called

18.

Point out the error in the program?


#include int main( )

{char ch;

int I;

scanf(“%c”, &i);

scanf(“%d”,&ch);

printf(“%c””%d”,ch,i);

return 0;}

a) No error
b) Error: Suspicious char to inversion in scanf()
c) None
d) Error: We may get input for second scanf() statement
Output:  A No error

19.

What is the output of the following code?

Void my_recursive_function(int n)

{if(n==0)return;

printf(“%d”,n); }

int main(){

my_recursive_function(10);

return 0;}

a) 10 9 8 …1 0
b) 10 9 8 …1
c) 1
d) 10

Output: d) 10

20. What are character constants in C++?


Ans Character constants are one or more members of the “source character set,”
the character set in which a program is written, surrounded by single quotation
marks ('). They are used to represent characters in the “execution character set,”
the character set on the machine where the program executes. These are
sometimes also called character literals.

In C++, A character literal is composed of a constant character. It is represented


by the character surrounded by single quotation marks. There are two kinds of
character literals −

Narrow-character literals of type char, for example, 'a'


Wide-character literals of type wchar_t, for example, L'a'
The character used for a character literal may be any graphic character, except
for reserved characters such as newline ('\n'), backslash ('\'), single quotation
mark ('), and double quotation mark ("). Reserved characters are be specified
with an escape sequence.
21.Write a program to add two strings without utilizing “+” operator ?
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main() {
char s1[50], s2[30];
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("\nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[]) {
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}

22.Define the terms OSI, TCP, and IP.


Ans TCP: Stands for "Transmission Control Protocol." TCP is a
fundamental protocol within the Internet protocol suite — a collection of standards
that allow systems to communicate over the Internet. It is categorized as a
"transport layer" protocol since it creates and maintains connections between hosts.
TCP compliments the Internet protocol (IP), which defines IP addresses used to
identify systems on the Internet. The Internet protocol provides instructions for
transferring data while the transmission control protocol creates the connection and
manages the delivery of packets from one system to another. The two protocols are
commonly grouped together and referred to as TCP/IP.
When data is sent over a TCP connection, the protocol divides it into individually
numbered packets or "segments." Each packet includes a header that defines the
source and destination and a data section. Since packets can travel over the Internet
using multiple routes, they may arrive at the destination in a different order than
they were sent. The transmission control protocol reorders the packets in the
correct sequence on the receiving end.
TCP also includes error checking, which ensures each packet is delivered as
requested.
IP: Stands for "Internet Protocol." IP provides a standard set of rules for sending
and receiving data over the Internet. It allows devices running on different
platforms to communicate with each other as long as they are connected to the
Internet.
In order for a Internet-connected host to be recognized by other devices, it must
have an IP address. This may be either an IPv4 or IPv6 address, but either way it
uniquely defines a device on the Internet.

The Internet Protocol also provides basic instructions for transferring packets
between devices. However, it does not actually establish the connection or define
the ordering of the packets transmitted. These aspects are handled by the
Transmission Control Protocol, which works in conjunction with the Internet
Protocol to transfer data between systems on the Internet. For this reason,
connections between Internet-connected systems are often called "TCP/IP"
connections.
OSI: The OSI (Open Systems Interconnection) model was created by the ISO to help
standardize communication between computer systems. It divides communications
into seven different layers, which each include multiple hardware standards, protocols,
or other types of services.
The seven layers of the OSI model include:

1. The Physical layer


2. The Data Link layer
3. The Network layer
4. The Transport layer
5. The Session layer
6. The Presentation layer
7. The Application layer
When one computer system communicates with another, whether it is over a local
network or the Internet, data travels through these seven layers. It begins with the
physical layer of the transmitting system and travels through the other layers to the
application layer. Once the data reaches the application layer, it is processed by the
receiving system. In some cases, the data will move through the layers in reverse to
the physical layer of the receiving computer.

The best way to explain how the OSI model works is to use a real life example. In the
following illustration, a computer is using a wireless connection to access a secure
website.

The communications stack begins with the (1) physical layer. This may be the
computer's Wi-Fi card, which transmits data using the IEEE 802.11n standard. Next,
the (2) data link layer might involve connecting to a router via DHCP. This would
provide the system with an IP address, which is part of the (3) network layer. Once the
computer has an IP address, it can connect to the Internet via the TCP protocol, which
is the (4) transport layer. The system may then establish a NetBIOS session, which
creates the (5) session layer. If a secure connection is established, the (6) presentation
layer may involve an SSL connection. Finally, the (7) application layer consists of the
HTTP connection to the website.

The OSI model provides a helpful overview of the way computer systems
communicate with each other. Software developers often use this model when writing
software that requires networking or Internet support. Instead of recreating the
communications stack from scratch, software developers only need to include
functions for the specific OSI layer(s) their programs use

24. There is a class which contains two integers as private members. There
are two member functions (public) defined on it, one to add the two
integers and another to subtract the two integers. Ravi wants to add a new
functionality, which enables multiplication of the two numbers. Which one
of the following options he should adopt?
a) He should define a third member function (public) which multiplies the two
numbers.
b) He should define member functions (public) to return the value of both the
integers and then multiply them in his code. By returning the values, he can in
future do any operation on them giving extensibility to the code.
c) He should define a third member function (private) which multiplies the
two numbers.
d) He should define member functions (private) to return the value of both
the integers and then multiply them in his code. By returning the values, he
can in future do any operation on them giving extensibility to the code.

Answer a) He should define a third member function (public) which


multiplies the two numbers.

Q 26. Implementing Stack using Arrays (5 marks).


#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{ if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << " Popped from stack\n";
return 0;
}
Output :

10 pushed into stack


20 pushed into stack
30 pushed into stack
30 popped from stack

Q 27. Input a matrix (M*N) and print the row contain maximum sum (5
marks)
Example:

1 2 3 ==6(sum of element)

4 5 6 ==15

7 8 9 ==23

Ans: 7 8 9 abhi thode changes hai isme subha bhej dunga

#include <bits/stdc++.h>

using namespace std;

#define N 5 // No of rows and column

// Function to find the row with max sum

pair<int, int> colMaxSum(int mat[N][N])

// Variable to store index of row

// with maximum

int idx = -1;

// Variable to store max sum

int maxSum = INT_MIN;

// Traverse matrix row wise

for (int i = 0; i < N; i++) {

int sum = 0;
// calculate sum of row

for (int j = 0; j < N; j++) {

sum += mat[i][j];

// Update maxSum if it is less than

// current sum

if (sum > maxSum) {

maxSum = sum;

// store index

idx = i;

pair<int, int> res;

res = make_pair(idx, maxSum);

// return result

return res;

}
// Driver code

int main()

int mat[N][N] = {

{ 1, 2, 3, 4, 5 },

{ 5, 3, 1, 4, 2 },

{ 5, 6, 7, 8, 9 },

{ 0, 6, 3, 4, 12 },

{ 9, 7, 12, 4, 3 },

};

pair<int, int> ans = colMaxSum(mat);

cout << "Row " << ans.first + 1 << " has max sum "

<< ans.second;

return 0;

You might also like