0% found this document useful (0 votes)
29 views18 pages

Sample Paper With Answers

Uploaded by

Faizan Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views18 pages

Sample Paper With Answers

Uploaded by

Faizan Ahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Sample Paper

FINALTERM EXAMINATION
Fall 2022
CS201 – Introduction to Programming

Time: 90 min
Marks: 60

Question No: 1 (Marks: 01) - Please choose the correct option


Answer: tellg()

…………. function is used to determine the current location while reading a file.

A. tellp()
B. tellg()
C. seekp()
D. seekg()

Question No: 2 (Marks: 01) - Please choose the correct option


Answer: ^

…………. operator can be used for masking of flags.

A. ^
B. &&
C. ||
D. >>

Question No: 3 (Marks: 01) - Please choose the correct option


Answer: 20

What will be the output of the following code?

int main(){
int i = 20;

for(int i=1;i<10;i++);
cout<<i;

return 0;
}

A. 10
B. 20
C. 9
D. 1

Question No: 4 (Marks: 01) - Please choose the correct option


Answer: 24

What will be the output of the following code?

int main() {
change();
return 0;
}
int change (int a=12){
int b=12;
a = a + b;
cout<<a++;
}

A. 12
B. 24
C. 25
D. 26

Question No: 5 (Marks: 01) - Please choose the correct option


Answer: ios::in

While handling files in C++, the default mode of “ifstream” library for opening file is.
A. ios::ate
B. ios::out
C. ios::in
D. ios::app

Question No: 6 (Marks: 01) - Please choose the correct option


Answer: Single line

In C++, “getline()” function is a built-in function that allows accepting and reading.
A. Single line
B. Multiple lines
C. Single and Multiple lines
D. Only multiword input

Question No: 7 (Marks: 01) - Please choose the correct option


Answer: struct Student {char name[30];}

Which one of the following is correct definition of structure?


A. struct Student{char name[30]}
B. struct Student {char name[30];}
C. struct Student {char name[35];};
D. struct {char name[30]};

Question No: 8 (Marks: 01) - Please choose the correct option


Answer: sptr->title

struct book {
char title[20];
};

int main(){
book * sptr;
}

From the above code the correct way to access the data member title of the structure
book through pointer is?

A. sptr.title
B. sptr:title
C. sptr->title
D. .sptr*title

Question No: 9 (Marks: 01) - Please choose the correct option


Answer: 6
What will be the output of the following program?

#include <iostream>
#define LIMIT 3

int main()
{
for (int i = 2; i < LIMIT; i++) {
std::cout << i* LIMIT << "\t";
}
return 0;
}
A. 6
B. 2 3 6
C. 3
D. 9

Question No: 10 (Marks: 01) - Please choose the correct option


Answer: #define SIZE 5

The proper way of defining a macro is.


A. #define SIZE 5;
B. #define SIZE 5
C. Set SIZE 5;
D. #set SIZE 5

Question No: 11 (Marks: 01) - Please choose the correct option


Answer: Heap

In C++, dynamic memory allocation in done on _____ memory.

A. Static
B. Stack
C. Heap
D. Hard Disk

Question No: 12 (Marks: 01) - Please choose the correct option


Answer: pointer
Which of the following is not an acceptable class specifier?
A. public
B. private
C. protected
D. pointer

Question No: 13 (Marks: 01) - Please choose the correct option


Answer: Attribute

_____ is another name for a class's data members.

A. Attribute
B. Method
C. Instance
D. Dimensions

Question No: 14 (Marks: 01) - Please choose the correct option


Answer: private

In C++, class data members are generally kept as ______.


A. public
B. private
C. protected
D. static

Question No: 15 (Marks: 01) - Please choose the correct option


Answer: friend

To declare the friend function, which keyword is used?


A. Friend
B. FRIEND
C. FrienD
D. friend

Question No: 16 (Marks: 01) - Please choose the correct option


Answer: /

In C++, which of the following operator cannot be overloaded?

A. +
B. -
C. /
D. ->

Question No: 17 (Marks: 01) - Please choose the correct option


Answer: Member functions

A class includes both data members as well as functions to manipulate the data. These
functions are called ___________.

A. Member functions
B. Constant functions
C. Rational functions
D. Memory functions

Question No: 18 (Marks: 01) - Please choose the correct option


Answer: Object

Which of the following is known as an instance of a class?

A. Data member
B. Constructor
C. Member function
D. Object

Question No: 19 (Marks: 01) - Please choose the correct option


Answer: ::
In friend function, which operator is utilized to access members of a class using class
objects.

A. ::
B. +
C. *
D. .

Question No: 20 (Marks: 01) - Please choose the correct option


Answer: this->buf

Which of the following is the correct way of referring a data member “buf” of a class
name “String”? Consider “buf” data member declared as a character pointer.

A. buf;
B. this->buf;
C. *(this).buf;
D. *this.buf

Question No: 21 (Marks: 01) - Please choose the correct option


Answer: %

In C++, which of the following operator can be overloaded?

A. sizeof
B. ?:
C. ::
D. %

Question No: 22 (Marks: 01) - Please choose the correct option


Answer: Zero

When a unary operator is overloaded and is a member of a class, how many arguments
need to be passed explicitly when it is called?

A. Zero
B. One
C. Two
D. Three

Question No: 23 (Marks: 01) - Please choose the correct option


Answer: 10

What will be the output of following program in C++?

#include<iostream>
using namespace std;
class A
{
int a;
public:
A(int x):a(x){}
int geta() {return a;}
};
int main ()
{
A M(10),N(20);
N = M;
cout<<N.geta();
return 0;
}

A. 20
B. 10
C. 30
D. 15

Question No:24 (Marks: 01) - Please choose the correct option


Answer: ends

Which of the following manipulators “Terminate a string with NULL”?

A. endl
B. flush
C. ends
D. ws

Question No: 25 (Marks: 01) - Please choose the correct option


Answer: 3.144

What will be the output of following code snippet written in C++?


int main(){
double var = 3.14362987;
cout<< setprecision(4) << var;
}

A. 3.144
B. 3.143
C. 3.1436
D. 3.145

Question No: 26 (Marks: 01) - Please choose the correct option


Answer: ios::showcause

Which of the following is not a format state flag?

A. ios::dec
B. ios::showpoint
C. ios::showcause
D. ios::right

Question No: 27 (Marks: 01) - Please choose the correct option


Answer: In passing by reference

Which of the following is not a recommended way to use a copy constructor?

A. Explicit call to the copy constructor


B. Using “=” operator
C. In passing by value
D. In passing by reference
Question No: 28 (Marks: 01) - Please choose the correct option
Answer: character

If we press the key 1 from the keyboard to store it in integer i, the digit 1 travels as a(an)
_________ but stored as number inside i.

A. integer
B. character
C. float
D. double

Question No: 29 (Marks: 01) - Please choose the correct option


Answer: Source and destination

In C++, for every stream, there should be ________.

A. Template and class


B. Source and destination
C. Function and operator
D. Structure and Union

Question No: 30 (Marks: 01) - Please choose the correct option


Answer: Reference

In C++, whenever have an “&” ampersand symbol appears on right hand side of an “=”
assignment operator, it is taken as ______ of an object.

A. Pointer
B. Reference
C. Address
D. Function

Question No: 31 (Marks: 01) - Please choose the correct option


Answer: Instance
While writing nested classes in C++, no memory is allocated for it unless a/an _______
of it is created.

A. Function
B. Instance
C. Class
D. Operator

Question No: 32 (Marks: 01) - Please choose the correct option


Answer: Dangling Reference

In C++, which one from the following errors can be encountered when dealing with
references.

A. Memory Leak
B. Dangling Reference
C. Static Reference
D. Global Reference

Question No: 33 (Marks: 01) - Please choose the correct option


Answer: Function overloading

In C++, operator overloading is quite similar to _______.

A. Macros.
B. Inline function
C. Function overloading
D. Object Creation

Question No: 34 (Marks: 01) - Please choose the correct option


Answer: protected and private members can be accessed from outside the
class

Considering knowledge of using a friend functions in C/C++, choose which rule is


applicable on friend functions?

A. private members cannot be accessed from anywhere


B. protected members cannot be accessed from anywhere
C. protected and private members can be accessed from outside the class
D. protected and private members cannot be accessed from outside the class

Question No: 35 (Marks: 01) - Please choose the correct option


Answer: template <class T> class student { …. };

The correct syntax for writing a template ‘class student’ of type ‘T’ is _____.
A. template <class T> class student { …. };
B. template <Class T> Class student{ …. };
C. Template <class T> class student { …. };
D. template <class student > class T { …. };

Question No: 36 (Marks: 01) - Please choose the correct option


Answer: 9

In C/C++, the array index number for representing last element of an array of size 10 is
_____.
A. 5
B. 7
C. 9
D. 11

Question No: 37 (Marks: 01) - Please choose the correct option


Answer: 5

Trace the output of the following code segment.


int main(){
int arr[2][3]= {{1, 2, 3}, {4, 5, 6}}
cout<<arr[1][1];

A. 2
B. 4
C. 5
D. 6

Question No: 38 (Marks: 01) - Please choose the correct option


Answer: 10

What will be the output of the given code segment?

int arr[]={2,5,6,8,9,10,6,3,1};
int * ptr= &arr[1];
cout<<*(ptr+4);

A. 6
B. 10
C. 3
D. 1

Question No: 39 (Marks: 01) - Please choose the correct option


Answer: 6

What will be the output of the given code segment?

int arr[]={2,5,6,8,9,10,6,3,1};
int * ptr= &arr[3];
cout<<*(ptr+4);

A. 10
B. 6
C. 3
D. 5

Question No: 40 (Marks: 01) - Please choose the correct option


Answer: friends

Which one of the following is not a keyword and can be used as an identifier in C/C++?
A. friends
B. template
C. this
D. new

Question No: 41 (Marks: 03)

Answer: 6 iterations

Determine the number of iterations the following loop will run.

int op, lim, k;


op = 6;
lim = 0;
while (k <= 60) {
k = op << lim;
lim += 1;
}

Question No: 42 (Marks: 03)

Answer: Answer: ^ operator

What is the masking operator? Write the code snippet and determine the output of
expression which masks a variable int x = 100; with int m = 50;

Question No: 40 (Marks: 03)

Answer: friends

What will be output of the following code segment?

class Output{
private:
int a,b,c;
public:
Output(){
a = 8;
b = 4;
c = 0;
}
void Display(){
c=a/b;
cout <<a<<"\n"<<b<<"\n"<<c;
}
};
int main() {
Output ob;
ob.Display();
return 0;
}

Question No: 44 (Marks: 03)


Answer: Default, parameterized, copy

Name the types of constructor used in C++.

Question No: 45 (Marks: 03)


Answer: cin, cout

Write the keywords used for auxiliary input output stream and printer output
stream in DOS.

Question No: 46 (Marks: 05)


Answer: 1.234e+02

What will be output of the following program?

#include <iostream>
using namespace std;
int main () {

double x = 123.4;
cout.precision(4);
cout<< scientific;
cout<< x << endl;
return 0;

}
Question No: 47 (Marks: 05)

Answer: sum=15

What will be the output of the following code written in C++. (Assuming memory is
successfully allocated in both calls)

int main(){
int *ptr, sum=0, i=0, n=3;

ptr = (int *) malloc (n * sizeof(int));

if (ptr==NULL) {
cout<<"Memory allocation is not successful";
return 1;
}
while (i<=n) {
*(ptr+i) = i;
i++;
}

n=5;

ptr = (int *) realloc (ptr, n * sizeof(int));

if (ptr == NULL){
cout<<"Memory allocation is not successful";
return 1;
}
for (int j = 0;j < = n; j++){
*(ptr + j ) = j;
sum +=*( ptr + j);
}
cout<<"sum="<<sum;
free(ptr);
return 0;
}

Question No: 48 (Marks: 05)

Answer: Variable = 0
Variable = 10
Trace the output of the following code.

class first
{
friend void add(first *, int);
private:
int variable;
public:
void print()
{
cout << "Variable = " <<variable<<endl;
}
first();
};
first::first()
{
variable = 0;
}
void add(first *x, int y){
x->variable += y;
}

int main(){
first z;
z.print();
add(&z, 10);
z.print();
}

Question No: 49 (Marks: 05)


Answer: int* arr = new int[size]; delete[] arr;

Write the C++ syntax to create and delete a dynamic array using new and delete operator.

Question No: 50 (Marks: 05)

Answer: Class One Constructor


Class Three Constructor
Class Two Constructor
Class three Destructor
Class One Destructor

Carefully read and analyze the following code and determine its output.
class One {
public:
One(){
cout << "Class One Constructor" << endl;
}
~One(){
cout << "Class One Destructor" << endl;
}
};
class Three {
private:
One one;
public:
Three() {
cout << "Class Three Constructor" << endl;
}
~Three(){
cout << "Class three Destructor" << endl;
}

};
class Two {

private:
Three three;
public:
Two(){
cout << "Class Two Constructor" << endl;
}
};

int main()
{
Two two;
return 0;
}

You might also like