Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

c++ paper

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Your Roll No………………………

Sr. No. of Question Paper:

Unique Paper Code: 2344001101

Name of the paper: Programming using C ++

Name of the Course: Generic Elective (NEP-UGCF-2022)

Semester: V

Duration: 3 Hours Maximum Marks: 90

Instructions for Candidates

1. Write your Roll No. on the top immediately on receipt of this question paper.
2. Section A is compulsory.
3. Attempt any four questions from Section B.
4. Parts of a question must be answered together.
Section A

Q1. a) What is the output of the following codes:


i) for (int i = 10; i > 6; i = i - 2) (3)
cout << i<<endl;
for (int i = -5; i > -7; i--)
cout << i + 1;

ii) int main() (3)


{
int x = 5;
int* p = &x;
int** pp = &p;
cout << **pp << endl;
return 0;
}
iii) int main() { (3)
int x = 2;
switch (x) {
case 1:
cout << "Case 1" << endl;
case 2:
cout << "Case 2" << endl;
case 3:
cout << "Case 3" << endl;
default:
cout << "Default case" << endl;
}
return 0;
}
iv) int arr[] = { 4, 5, 6, 7 }; (3)
int* p = (arr + 1);
cout << *arr + 10<<end;;
cout<<*p;

b) Identify the errors in the following programs:

i) int x; (2)
x+3=x;
cout<<x<<y;

ii) void fun1(int x, int y) (2)


{
int c;
c=a+b;
return c;
}

d) Given the following declaration: (5)


int i1, i2;
float f1;
char c1, c2;
and following line of data user enters
45.8 78 34 Hello
What would be the value of i1, i2, f1, c1 and c2 after the following statement
cin >> I1 >> i2 >> f1 >> c1 >> c2;

e) When we should use an inline function? Explain with suitable example. (4)
f) Rewrite the following code with the while loop: (3)
int main()
{
char letter = 'a';

for (int i = 97; i < 123; i = i + 1)


{
cout << letter << '\t' << i << '\n';
letter = letter + 1;
}
}

g) Which of the following are not valid identifiers: (2)


i) tax-rate ii) int iii) num iv)
Section- B

Q2. (a) What is copy constructor? Explain it with suitable example. (4)
(b) Rewrite the following code fragment using a switch statement: (3)
if(ch == 'C' II ch == 'c')
countC++;
elseif(ch =='A' II ch =='a')
countA++;
elseif(ch =='J' II ch == 'j')
countJ++;
else
cout << "Error- Not C, A, or J \n";

c) Create a base class Person, a derived class Employee, and a further derived class (8)
Manager. Show how each class inherits attributes and methods from its parent class.
Define main function to print all information about an object of class Manager.

3 a) Write a program to print the following pattern: (5)


1
23
456
7 8 9 10

Program should accept the number of lines n as the input from the user.

b) Write a C++ program to handling of multiple exception types. Use try and catch (6)
blocks to catch different types of exceptions, such as out_of_range and
invalid_argument. The program should handle these exceptions appropriately and
display an error message based on the exception type.

c) Write a function that tests the value of an integer num. If the num = 10 then square (4)
num. If num =9 , read a new value into num. if num=4 or num = 5, multiply num by
50 and print the result. Use if statement.

Q4. a) Write a function getData() that takes an integer, a float, and a string as arguments. (5)
Overload the function so that the program prints the correct data type (Integer, Float,
or String) with the value passed.

b) What is a Class Template in C++? Write a C++ program to implement a simple (5)
calculator that performs basic operations (addition, subtraction, multiplication, and
division) using a class template.
c) Write a C++ program to count the number of words in a file (5)
Q5. a) Create a base class Student with a virtual function getMarks(). Then, create two (8)
derived classes Faculty and Admin that override the getMarks() function. In the
main() function, Use runtime polymorphism by calling getMarks() through base
class pointers.

b) Write a function to show the difference between call by value and call by reference (3)
while swapping two numbers

c) What will be the output of the following code: (4)


Class P
{
public:
P()
{
cout<<”Class P constructor;
}
~ P()
{
cout<<”Class P destructor;
}
};
Class Q : public P
{
public:
Q()
{
cout<<”Class Q Constructor;
}
~ Q()
{
cout<<”Class Q Destructor;
}
};
void main()
{
Q t;
}

Q6. a) Explain the difference between multiple inheritance and multilevel inheritance in (5)
C++. Provide examples to illustrate both concepts.

b) Create a class Complex that represents a complex number with two integer data (10)
members: real and imag. Implement the following:
• A default constructor that initializes the complex number to 0.
• A parameterized constructor that allows initialization of the complex
number with specific real and imag values.
• A function addComplex() to add two complex numbers and return the result
as a new complex number.
• A function display() to display the result of complex number.

Write a program to create a class by adding two complex numbers and displaying
the result.

Q7. a) Write a recursive function to get the nth Fibonacci number. (5)
b) Define a class Rectangle with two data members length and width, and a function (5)
setDimensions() that sets the values of these members. Use this pointer to refer to
the members.

c) Explain the use of following operators with a suitable example: (5)


i) =
ii) !=
iii) ||
iv) ::
v) .

You might also like