Education In Pakistan

Papers, Notes, Books & Help For Students

UPDATED EDUCATIONAL NEWS INTERVIEW HELP FOR ALL JOBS ONLINE BOOKS SCHOLARSHIPS AVAILABLE INTERNSHIP JOBS

Tag: VU CS201 -Introducing to programming MIDTERM Solved/Unsolved Papers Spring 2010

VU CS201 -Introducing to programming MIDTERM Solved/Unsolved Papers Spring 2010

VU CS201 -Introducing To Programming MIDTERM Solved/Unsolved Papers Spring 2010

Online Quiz C++ Fall-2009

Q1: ofstream is used for________

  • Input file stream
  • Output file stream
  • Input and output file stream
  • All of the given

Q2: Function prototype is written,

  • Within main function
  • After the return statement in main
  • Before the return statement in main
  • Before call of that function

Q3: RAID stands for__________________.

  • Redundant Array of Inexpensive Dices
  • Redundant Array of Inexperience Devices
  • Redundant Array of Inexpensive Devices
  • Reduced Array of Inexpensive Devices

Q4: If int sum = 54; Then the value of the following statement is sum = sum – 3 ;

  • 52
  • 50
  • 51
  • 57

Q5: In while loop the loop counter must be initialized,

  • With in the loop
  • Before entering the loop
  • At the end of the loop
  • None of the given options

Q6: Computer can understand only __________language code.

  • High level
  • Low level
  • Machine 
  • Fourth generation

Q7: Transpose of a matrix means that when we interchange rows and columns_____________

  • the first row becomes the Last column
  • the first row becomes the first column
  • the Last row becomes the first column
  • the first column becomes the first row

Q8: Loops are _______________ Structure

  • Decision
  • Sequential
  • Repetition
  • None of the given options

Q9: C is widely known as development language of _______ operating system.

  • Linux
  • Unix
  • Windows
  • Mac OS

Q10: In_________, we try to have a precise problem statement

  • Analysis
  • Design
  • Coding
  • None of the given

 

 

Q11: _________ Keyword is used to return some value from a function.

  • break
  • return
  • continue
  • goto

Q12: Pointers store the ____________________

  • value of a variable
  • memory address
  • characters
  • None of the given

Q13: < , <= , > , >= are called __________ operators.

  • Arithmetic
  • Logical
  • Relational
  • Conational

Q14: the sizeof operator is used to determine the size of _____________.

  • data
  • variable
  • data type
  • None of the given  (Correct Answer is Delete operator)

Q15: If int a = 50; then the value of a/= 3; will be,

  • 15
  • 18
  • 16
  • 17

Q16: _______________are conventional names of the command line parameters of the ‘main()’ function.

  • ‘argb’ and ‘argv’
  • ‘argc’ and ‘argv’
  • ‘argc’ and ‘argu’
  • None of the given

Q17: From following; which one is the correct syntax of an array initialize: Array size is 10 and it is of double data type to value 0?

  • arr[10] = {0.0};
  • double arr[10]= 0.0;
  • double arr[10] = {0.0};
  • double arr[] = 0.0;

Q18: ________ statement interrupts the flow of control.

  • switch
  • continue
  • goto
  • break

Q19: From the following; which one is used as an assignment operator?

    • Equal sign ‘=’
    • Double equal sign ‘==’
    • Both equal and double equal sign
  • None of the given options

FINALTERM  EXAMINATION  Spring 2009

CS201- Introduction to Programming

Q1: There are mainly ——————– types of software

       ► Two

► Three

► Four

► Five

Q2: When x = 7; then the expression x%= 2; will calculate the value of x as,

       ► 1

► 3

► 7

► 2

Q3: A pointer variable can be,

► Decremented only

► Incremented only

► Multiplied only

 ► Both 1 and 2

àQ4: set precision is a parameter less manipulator.

    ► True

       ► False

Q5: We can change a Unary operator to Binary operator through operator overloading.

    ► False

► True

Q6: delete operator is used to return memory to free store which is allocated by the new operator

    ► True

       ► False

àQ7: When we do dynamic memory allocation in the constructor of a class, then it is necessary to provide a destructor.

   ► True

       ► False à delete(p);

àQ8: What is the functionality of the following statement?

String str[5] = {String(“Programming”), String(“CS201”)};

  • Default constructor will call for all objects of array
  • Parameterized constructor will call for all objects of array
  • Parameterized constructor will call for first 2 objects and default constructor for remaining        objects
  • Default constructor will call for first 3 objects and Parameterized constructor for remaining objects

Q9: What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor

► Constructor is called first before allocating memory

Q10: Deleting an array of objects without specifying [] brackets may lead to memory leak

 ► True

       ► False

Q11: Which of the following data type will be assumed if no data type is specified with constant?

► short

► float

 ► int

       ► double

Q12: There is an array of characters having name ‘course’ that has to be initialized by string ‘programming’ which of the following is the correct way to do this,

  1. i.  course[] = {‘p’, ’r’, ’o’, ’g’, ’r’, ’a’, ’m’, ’m’, ’i’, ’n’, ’g’};

ii.course[] = ‘programming’ ;

  1. iii.    course[12] = “programming” ;
  2. iv.    course = “programming” ;

Choose the correct options.

► (i) and (ii) only

► (i) and (iv) only

       ► (i) and (iii) only

► (ii) and (iii) only

Q13: What will be the correct syntax of the following statement?

ptr is a constant pointer to integer.

► const int *ptr ;

► const *int ptr ;

► int const *ptr ;

       ► int *const ptr ; 

Q14: Overloaded member operator function is always called by _______

► Class

       ► Object

► Compiler

► Primitive data type

Q15: Loader loads the executable code from hard disk to main memory.

       ► True

► False

Q16: Which of the following is the correct C++ syntax to allocate space dynamically for an array of 10 int?

► new int(10) ;

       ► new int[10] ;

► int new(10) ;

► int new[10];

Q17: The prototype of friend functions must be written ____ the class and its definition must be written ____

à    ► inside, inside the class

inside, outside the class

       ► outside, inside the class

► outside, outside the class

Q18: Like member functions, ______ can also access the private data members of a class.

► Non-member functions

       ► Friend functions

► Any function outside class

► None of the given options

Q19: To perform manipulation with input/output, we have to include _____ header file.

       ► iostream.h

► stdlib.h

► iomanip.h

► fstream.h

Q20: The endl and flush are _______

► Functions

► Operators

       ► Manipulators

► Objects

Q21: If we want to use stream insertion and extraction operators with _______ then we have to overload these operators.

► int, float, double

 ► objects of class

       ► int, float, object

► int, char, float

Q22: The static data members of a class can be accessed by ________

 ► only class

       ► only objects

► both class and objects

► none of given options

Q23: Classes defined inside other classes are called ________ classes

► looped

       ► nested

► overloaded

► none of the given options.

Q24: Which value is returned by the destructor of a class?

► A pointer to the class.

► An object of the class.

► A status code determining whether the class was destructed correctly

       ► Destructors do not return a value.

Q25: Consider the following code segment

class M {

friend int operator!(const M &);

};

!s                                 // code of line implies that     operator!(s)

Let assume if s is an object of the class then function is implemented as ___________

► Member function

à  ► Non-member function

       ► Binary operator function

► None of the given options

Q26: When the compiler overloads the assignment (=) operator by default then __________

► compiler does member wise assignment.

► compiler does not allow default overload of assignment (=) operator

 ► member of the class are not assigned properly

       ► None of the given options

Q27: If text is a pointer of class String then what is meant by the following statement?

text = new String [5];

► Creates an array of 5 string objects statically

► Creates an array of 5 string objects dynamically

► Creates an array of pointers to string

► Creates a string Object

Q28: Static variable which is defined in a function is initialized __________.

       ► Only once during its life time

► Every time the function call

► Compile time of the program

► None of the above

Q29: The appropriate data type to store the number of rows and colums of the matrix is____________.

► float

int

► char

► none of the given options.

Q30: Copy constructor becomes necessary while dealing with _______allocation in the class.

Dynamic memory

       ► Static memory

► Both Dynamic and Static memory

       ► None of the given options

FINALTERM  EXAMINATION (Fall 2008)

Q1: There are mainly ——————– types of software

       ► Two

► Three

► Four

► Five

Q2: seekg() and write() are functionally _________________ .

à  ► Different

► Identical

► Two names of same function

► None of the above

Q3: When a pointer is incremented, it actually jumps the number of memory addresses

       ► According to data type

► 1 byte exactly

► 1 bit exactly

► A pointer variable can not be incremented

Q4: setw is a parameterized manipulator.

       ► True

► False

Q5: eof( ), bad( ), good( ), clear( ) all are manipulators.

       ► True  à Stream Manipulators

► False

Q6: In functions that return reference, use __________variables.

► Local

► Global

       ► Global or static

► None of the given option

Q7: The declarator of Plus (+) member operator function is

  ► Class-Name  operator + (Class-Name  rhs)

► operator Class-Name + ( )

► operator Class-Name + ( rhs)

► Class-Name  operator + ( )

Q8: The compiler does not provide a copy constructor if we do not provide it.

► True

 ► False

Q9: What is the functionality of the following syntax to delete an array of 5 objects named arr allocated using new operator?

delete arr ;

► Deletes all the objects of array

► Deletes one object of array

Do not delete any object

► Results into syntax error

Q10: What is the sequence of event(s) when allocating memory using new operator?

► Only block of memory is allocated for objects

► Only constructor is called for objects

► Memory is allocated first before calling constructor

  ► Constructor is called first before allocating memory

Q11: What is the sequence of event(s) when deallocating memory using delete operator?

► Only block of memory is deallocated for objects

► Only destructor is called for objects

► Memory is deallocated first before calling destructor

     ► Destructor is called first before deallocating memory

Q12: new and delete operators cannot be overloaded as member functions.

► True

       ► False

Q13: The operator function of <<  and >> operators are always the member function of a class.

► True

       ► False

Q14: A template function must have at least ———- generic data type

► Zero

       ► One

► Two

► Three

Q15: If we do not mention any return_value_type with a function, it will return an _____ value.

       ► int

► void

► double

► float

Q16: Suppose a program contains an array declared as int arr[100]; what will be the size of array?

► 0

► 99

       ► 100

► 101

Q17: The name of an array represents address of first location of array element.

       ► True

► False

Q18: Reusing the variables in program helps to save the memory

       ► True

► False

Q19: Which of the following option is true about new operator to dynamically allocate memory to an object?

► The new operator determines the size of an object

       ► Allocates memory to object and returns pointer of valid type

► Creates an object and calls the constructor to initialize the object

► All of the given options

Q20: new and delete are _____ whereas malloc and free are _____.

► Functions, operators

► Classes, operators

       ► Operators, functions

► Operators, classes

Q21: Like member functions, ______ can also access the private data members of a class.

► Non-member functions

       ► Friend functions

► Any function outside class

► None of the given options

Q22: Which of the following statement is best regarding declaration of friend function?

► Friend function must be declared after public keyword.

► Friend function must be declared after private keyword.

► Friend function must be declared at the top within class definition.

       ► It can be declared anywhere in class as these are not affected by the public and private keywords.

Q23: The operator function overloaded for an Assignment operator (=) must be

► Non-member function of class

       ► Member function of class

► Friend function of class

► None of the given options

Q24: For non-member operator function, object on left side of the operator may be

► Object of operator class

► Object of different class

► Built-in data type

► All of the given options

Q25: The operator function will be implemented as _____, if obj1 drive the – operator whereas obj2 is passed as arguments to – operator in the statement given below.

obj3   =   obj1 –   obj2;

       ► Member function

► Non-member function

► Friend function

► None of the given options

Q26: Which one of the following is the declaration of overloaded pre-increment operator implemented as member function?

► Class-name operator +() ;

► Class-name operator +(int) ;

       ► Class-name operator ++() ;

► Class-name operator ++(int) ;

Q27: The static data members of a class are initialized _______

► at file scope

► within class definition

► within member function

► within main function

Q28: Class is a user defined___________.

 ► data type

► memory referee

► value

► none of the given options.

Q29: We can also define a user-defines manipulators.

       ► True

► False

Q30: Automatic variable are created on ________.

► Heap

► Free store

► static storage

       ► stack

In C/C++ the string constant is enclosed

  • ? In curly braces
  • ? In small braces
  • ? In single quotes
  • ? In double quotes

The size of int data type is

  • ? 1 bytes
  • ? 2 bytes
  • ? 3 bytes
  • ? 4 bytes

In Flow chart process is represented by

  • ? Rectangle
  • ? Arrow symbol
  • ? Oval
  • ? Circle

If the break statement is missed in switch statement then,

  • ? The compiler will give error
  • ? This may cause a logical error
  • ? No effect on program
  • ? Program stops its execution

When we are using const keyword with a variable x then initializing it at the time of declaration is,

  • ? Must
  • ? Optional
  • ? Not necessary
  • ? A syntax error

Which of the following is the correct way to assign an integer value 5 to element of a matrix say ‘m’ at second row and third column?

  • ? m[2][3] = 5 ;   // [row][col]
  • ? m[3][2] = 5 ;
  • ? m[1][2] = 5 ;
  • ? m[2][3] = ‘5’;

How many total elements must be in two-dimensional array of 3 rows and 2 columns?

  • ? 4
  • ? 5
  • ? 6
  • ? 7

Which of the following is the correct statement for the following declaration?

const int *ptr.

  • ?ptr is a constant pointer
  • ?ptr is constant integer pointer
  • ?ptr is a constant pointer to int
  • ?ptr is a pointer to const int

Consider the following code segment. What will be the output of this code segment?   // 3 5

  • int arr[6] = {2, 3, 7, 4, 5, 6} ;
  • int *ptr1 =&arr[1] ;
  • int *ptr2 = &arr[4] ;
  • cout

What will be the correct syntax to assign an array named arr of 5 elements to a pointer ptr?

  • ?*ptr = arr ;
  • ?ptr = arr ;
  • ?*ptr = arr[5] ;
  • ?ptr = arr[5] ;

C is a/an ______ language

  • ?low level
  • ?object based
  • ?object oriented
  • ?function oriented

The variables having a name, type and size are just like empty boxes.

  • ?False
  • ?True

What’s wrong with this for loop?

for (int k = 2, k

FINALTERM EXAMINATION fall 2008

If Num is an integer variable then Num++ means,

  • Add 1 two times with Num
  • Add 1 with Num
  • Add 2 with Num
  • Subtract 2 from Num

If the return type of a function is void then it means that it will,

  • Return any type of data
  • Return some specific type of data
  • Return no data
  • Return just character data

Which of the following is a valid class declaration?

  • class A { int x; };
  • class B { }
  • public class A { }
  • object A { int x; };

When we use manipulators in our program then which header file should be included?

  • iostream.h
  • stdlib.h
  • stdio.h
  • iomanip.h

We can also create an array of user define data type.

  • True
  • False

The normal source of cin object is,

  • File
  • Disk
  • Keyboard
  • RAM

A stream is an ordered sequence of bytes.

  • True
  • False

We can delete an array of objects without specifying [] brackets if a class is not doing dynamic memory allocation internally.

  • True
  • False

The second parameter of operator functions for << and >> are objects of the class for which we are overloading these operators.

  • True
  • False

Which looping process checks the test condition at the end of the loop?

  • for
  • while
  • do while
  • no looping process checks the test condition at the end

In a group of nested loops, which loop is executed the most number of times?

  • the outermost loop
  • the innermost loop
  • all loops are executed the same number of times
  • cannot be determined without knowing the size of the loops

Template class can not have static variables.

  • True
  • àFalse

Consider the following statements to initialize a two-dimensional array.

i. int arr[2][3] = {4, 8, 9, 2, 1, 6} ;

ii. int arr[3][2] = {4, 8, 9, 2, 1, 6} ;

iii. int arr[][2] = {{4,8},{9, 2},{1, 6}} ;

Which of the following option(s) are correct to initialize a two-dimensional array with 3 rows and 2 columns?

  • (ii) only
  • (iii) only
  • (ii) and (iii)
  • and (iii)

There is a pointer variable named ptr of type int then address of which type of variable the ptr will store in it?

  • variable of type char
  • variable of type short
  • variable of type int
  • variable of type double

Let suppose

Union intorDouble{

Int ival;

Double charvar;

};

main(){

intorDouble VAZ;

int size ;

size = sizeof(VAZ);

}

What will be the value of variable “size”, if int occupies 4 bytes and double occupies 8 bytes?

  • 2
  • 4
  • 8
  • 12

new and delete are _____ whereas malloc and free are _____.

  • Functions, operators
  • Classes, operators
  • Operators, functions
  • Operators, classes

The member functions of a class occupy _____ region in memory for ____ object(s) of class.

  • separate, each
  • common, all
  • different, each
  • different, all

Friend functions are _____ of a class.

  • Member functions
  • Public member functions
  • Private member functions
  • Non-member functions

Which of the following is true while overloading operators?

  • Precedence of an operator can be changed
  • The parity (number of operands) can be changed
  • No new operators can be created
  • Associativity of an operator can be changed

Which of the following option will be true to overload the -= operator?

  • only – operator needs to be overloaded
  • Minus (-) and = operators need to be overloaded
  • the -= operator need to be overloaded explicitly
  • the – and = operators need to be overloaded implicitly

The input/output streams; cin and cout are ____

  • Operators
  • Functions
  • Objects
  • Structures

dec, hex, oct are all __________

  • Member functions
  • Objects of input/output streams
  • Parameterized manipulators
  • Non-parameterized manipulators

What will be the output of the following statement?

cout << setbase(16) << 52 ;

  • 74
  • 52
  • 34
  • 64

The first parameter of overloaded stream insertion operator is _________ where second parameter is _______

  • input stream, object of class
  • object of class, output stream
  • output stream, object of class
  • object of class, input stream

We can also do conditional compilation with preprocessor directives.

  • True
  • False

With user-defined data type variables (Objects), self assignment can produce __________.

  • Syntax error
  • Logical error
  • Link error
  • Non of the given options

The return type of the operator function for >> operator is ________.

  • class for which we overload this operator
  • reference of ostream class (ostream&)
  • reference of istream class (istream&)
  • void

When an object of a class is defined inside an other class then,

  • Constructor of enclosing class will be called first
  • Constructor of inner object will be called first
  • Constructor and Destructor will be called simultaneously
  • None of the given options

Where we can include a header file in the program?

  • any where
  • in start
  • at the end
  • none of the given options.

 

 

FINALTERM EXAMINATION SPRING 2006 – S2

Q1: A function that calls itself is known as

►Iterative function

►Inline function

Recursive function

►main ()

Q2: The function call to a default constructor

►looks like any function call, except there is no return value

►never takes any arguments

►creates but cannot initialize an object

is made automatically when an object is created

Q3: Static member functions

►must be declared inside the class definition, but defined outside it

►must be declared private

►have multiple copies for the entire class

can access only static data

Q4: The reserved words public and private comes under the category

►structures

►strings

accessibility modifiers

►types of functions

FINALTERM EXAMINATION SPRING 2006 CS201

A for loop usually consist of __________ expressions.

►1

3

►2

►4

A preprocessor directive is identified by _________ symbol

►#

►{

►(

►~

Which of the following operators can not be overloaded?

►new

►delete

►+=

sizeof

Analyze the following code

class myclass

{

private:

float x,y;

public:

void myclass(float a, float b)

{

x=a;

y=b;

}

void diplay()

{

cout<<endl<<x<<endl<<y;

}

};

What is wrong with the above code?

►The member functions should be private

constructor must not have a return type

►The constructor should have no body

►There is no error in the given code

 

Final Term Examination – February 2005

A function declaration has the same relationship to a function definition that

  • a class definition has to an object definition
  • an object declaration has to an object
  • a variable has to a variable declaration
  • a variable declaration has to the variable itself
  • they have no relationship

When we write a class template the first line must be:

  • template < class class_type>
  • template < class data_type>
  • template < class T >, Here T can be replaced with any name but it is preferable.
  • class class-name()
  • class template<class_name>

A friend function of a class has access

  • To all data member and functions of the class
  • Only to other friend functions of the class
  • Only to private data of the class
  • Only to public data of the class
  • To private data declared in main()

The new operator

  • is used to declare objects or variables
  • can not create and initialize an object
  • names an object or variable
  • returns an address to an object or variable
  • can allocate an appropriate amount of memory for an object or variable

If we open a file stream myfile for reading, what will give us the current position of the file pointer?

  • tellg()
  • tellp()
  • seekg()
  • seekp()
  • fstream.h

There is a class Student, Which one of the following is a valid destructor for this class.

  • Student();
  • Student(int);
  • ~ Student();
  • int~ Student();
  • ~ Student(int);

 

Final Term Examination – Spring 2005

A friend function

  • must be having a prototype with no arguments
  • must be invoked by an object of the class that declares it a friend
  • must be invoked by the class that declares it a friend
  • can access the private data of the class that declares it a friend
  • cannot access the data members of a class

Which one of the following operators is a unary operator?

  • OR ( || )
  • AND ( &&)
  • Complement operator ( ~ )
  • XOR ( ^ )
  • Insertion operator (>>)

If the statements

int j,k;

j = 123;

k= 234;

int* q, * r;

cout<<*q<<‘ ‘<<*r;

are executed, what will be displayed?

  • The values of j and k
  • The addresses of q and r
  • The addresses of j and k
  • 132 , 234
  • garbage values

A copy constructor

  • copies the data of any two constructors in that class
  • takes no arguments
  • takes an arbitrary number of arguments
  • creates a new object that later may be assigned the data of an existing object
  • creates an object initialized with the same data as an existing object

 

FINALTERM  EXAMINATION SPRING 2006

To access the 8th element of an int array named myArray of 15 elements, you would write

  • int[8]
  • int[7]
  • myArray[8]
  • myArray[7]

A copy constructor is always called when

  • an object is initialized with another object data of the same class when it’s created
  • an object is initialized with another object of any class when it’s created
  • an object is initialized with a variable of a basic type when it’s created
  • an object is not initialized when it’s created

Consider the function below:

template <class T>

T abc (T x)

{

return (-x);

}

We call this function as

cout << abc(-9.6) << endl;

The answer will be:

  • 9.6
  • -9.6
  • 9
  • -9

 

 

Constructor is itself a __________ of C++ and ___________.

  • class , can be overloaded
  • function , cannot be overloaded
  • function, can be overloaded
  • object, can not be initialized

The stream insertion and extraction operators are not already overloaded for ______

  • Built-in data types
  • User-defined data types ß
  • Both built-in and user-defined types
  • None of the given options

Look at the statement given below int & a; and tell what will happen ?

  • Compiler will compile successfully
  • Compiler will show a warning to you
  • Null value will be assigned to a
  • Compiler will generate an error: ‘a’ declared as reference but not initialized

< , <= , > , >= are called __________ operators.

  • Arithmetic
  • Logical
  • Relational
  • Conational

C++ provides member functions, which control the formatting to be performed during stream I/O operations.

  • True
  • False ß

The function will return a reference to the global variable that exists throughout the program and thus there will be no danger of _______________.

  • garbage collection
  • dangling reference.
  • wastage of memory
  • system crash

If an array has 50 elements, what is allowable range of subscripts?

  • 0 – 49 ß
  • 1 – 49
  • 0 – 50
  • 1 – 50

char name [] = “Hello World” ; In the above statement, a memory of _________ characters will be allocated

  • 13
  • 11
  • 12
  • 10

The destructor is used to ______________.

  • allocate memory
  • deallocate memory
  • create objects
  • allocate static memory

The first parameter of operator function for << operator _________________.

  • Must be passed by value
  • Must be passed by reference
  • Can be passed by value or reference
  • Must be object of class

 

 

 

 

 

 

 

 

 

 

 

 

 

 

VU CS201 -Introducing To Programming MIDTERM Solved/Unsolved Papers Spring 2010

VU CS201 -Introducing to programming MIDTERM Solved/Unsolved Papers Spring 2010

VU CS201 -Introducing To Programming MIDTERM Solved/Unsolved Papers Spring 2010

CS201 – Introduction to Programmming

Solved Subjective Questions

 

Question No: 1      ( Marks: 2 )

 

Write a declaration statement for an array of 10 elements of type float. Include an initialization statement of the first four elements to 1.0, 2.0, 3.0 and 4.0.

 

Answer:

float  floatArry[10] = {1.0,2.0,3.0,4.0};

 

Question No: 2      ( Marks: 2 )

 

Write the general syntax for the declaration of pre-increment and post-increment member operator function.

 

Answer:

Classname operator ++(); —- pre increment

Classname operator ++(int)  —- post increment

 

 

Question No: 3      ( Marks: 2 )

 

Give the general syntax of class template.

 

Answer:

template

class myclass { —} ;

 

Question No: 4      ( Marks: 2 )

 

What is a truth Table?

 

Answer:

There are some areas where the decision structures become very complicated. Sometimes, we find it difficult to evaluate a complicated logical expression. Sometimes the logic becomes extremely complicated so that even writing it as a simple syntax statement in any language. It becomes complicated to determine what will be evaluated in what way. We know the concept of truth table. The truth tables are very important. These are still a tool available for analyzing logical expressions. We will read logic design in future, which is actually to do with chips and gates. How we put these things together.

 

Question No: 5      ( Marks: 2 )

 

What will be the output of following code, if user input a number 123?

 

int input ;

cin >> oct >> input;

cout << hex << input ;

 

Answer:

 

53

 

Rational: it will take 123 as octal and print it in hex form which is 53.

 

 

Question No: 6      ( Marks: 2 )

 

What is principle of friendship in the context of functions and classes?

 

Answer:

Class can declare a friend function and someone from outside the class cannot declare itself friend of a class.

A friend function can access the private variables of class just like a member function

 

Question No: 7      ( Marks: 2 )

 

How many arguments a Unary Operator take? Can we make a binary operator as unary operator?

 

Answer:

Unary operator takes only one argument like i++ or i— (Post increment or post decrement operators for integers) or ++i,–i  (Pre increment or pre decrement operators for integers) ,we can not make Unary operator as binary or binary as Unary operator.

 

Question No: 8      ( Marks: 2 )

 

Which arithmetic operators cannot have a floating point operand?

 

Answer:

Modulus operator:

This operator can only be used with integer operands ONLY

 

Question No: 9      ( Marks: 2 )

 

What are manipulators? Give one example.

 

Answer:

The manipulators are like something that can be inserted into stream, effecting a change in the behavior. For example, if we have a floating point number, say pi (л), and have written it as float pi = 3.1415926 ; Now there is need of printing the value of pi up to two decimal places i.e. 3.14. This is a formatting functionality. For this, we have a manipulator that tells about width and number of decimal points of a number being printed.

 

Question No: 10      ( Marks: 2 )

 

Write down piece of code that will declare a matrix of 3×3. And initialize all its locations with 0;

 

Answer:

int matrix [3] [3] ;

 

include

 

 

main () {

int matrix [3][3];

int inivalue = 0;

for (int a=0;a<3;a++)

{ for (int b = 0;b<3;b++)

{ matrix[a][b]= inivalue;

cout<

}

 

Question No: 11      ( Marks: 2 )

 

What is the difference between switch statement and if statement.

Answer:

The “If” statement is used to select among two alternatives. It uses a Boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.

 

Question No: 12      ( Marks: 2 ) 

 

How can we initialize data members of contained object at construction time?

 

Answer:

Initializer list is used to initialize the contained objects at the construction time.

 

Question No: 13      ( Marks: 2 )

 

Can we overload new and delete operators?

 

Answer:

Yes, it is possible to overload new and delete operators to customize memory management. These operators can be overloaded in global (non-member) scope and in class scope as member operators.

 

Question No: 14      ( Marks: 2 )

 

Suppose there is a template function ‘func’ having argument of type U and return type T. What will be the C++ syntax to call this function, passing a variable ‘x’ of type double and returning an int type?

 

Answer:

template

T func (T a, U b) {

return (a

}

calling

int  i;

double x;

x = func

 

 

 

 

Question No: 15      ( Marks: 2 )

 

Which variable will be used in inner code block if we have the same names of variable at outer code block and inner code block?

 

Answer:

Simply: variable of the inner code is use in the inner code block.

 

Question No: 16      ( Marks: 2 )

 

 

What is the benefit of reference and where can we use it?

 

Answer:

In references we give the memory address of the object, due to references we pass values without making the copy. Hence, when we have many values & we want efficiency we use references to avoid copy.

 

 

Question No: 17      ( Marks: 2 )

 

Write the C++ code for the declaration of overloaded stream insertion and stream extraction operator for the object d of type Date.

 

Answer:

Date operator >> (date & d1){

cout<<<”-”<<<”-”<

}

 

 

 

Question No: 18      ( Marks: 2 )

 

What is difference between endl and \n? 

 

Answer:

Endl is manipulator and it inserts new line character and flushes the stream.

 

\n is control character which is used to insert line break.

 

Question No: 19      ( Marks: 2 )

 

What does code optimization mean?

 

Answer:

It is process by which we make our code in such a way that it improves the speed of program. By use of optimization we refine program codes in such a way that it run faster and consume less memory. We do it in such a way that output quality is not compromised.

 

Question No: 20      ( Marks: 3 )

 

How is the following cout statement interpreted by compiler?

 

cout << a << b << c ;

 

Answer:

It will give a compiler error because a,b,c are not declared.

 

Question No: 21      ( Marks: 3 )

 

Suppose an object of class A is declared as data member of class B.

 

(i)                                     The constructor of which class will be called first? 

Answer: A

(ii)                                   The destructor of which class will be called first?

Answer: B

 

 

Question No: 22      ( Marks: 3 )

 

What will be the output of following functions if we call these functions three times?

 

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

}

 

Answer:

 

1

1

1

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

 

Answer:

1

2

3

 

Question No: 23      ( Marks: 3 )

 

If is not available in the system then what does calloc/malloc and new operator return?

 

Answer:

calloc/malloc and new operator return returns a null pointer to indicate that no memory is available

 

 Question No: 24      ( Marks: 3 )

 

What is the keyword ‘this’ and what are the uses of ‘this’ pointer?

 

Answer:

‘this’ is use to refer the current class member without using the name of the class.

 

 

Question No: 25      ( Marks: 3 )

 

Which one (copy constructor or assignment operator) will be called in each of the following code segment?

 

1) Matrix m1 (m2);

2) Matrix m1, m2;

m1 = m2;

3) Matrix m1 = m2;

 

Answer:

 

1) Matrix m1 (m2);   copy constructor

2) Matrix m1, m2;

m1 = m2;               assignment operator

3) Matrix m1 = m2; assignment operator

 

Question No: 26      ( Marks: 3 )

 

What will be the output of following function if we call this function by passing int 5?

 

template T reciprocal(T x) {return (1/x); }

 

Answer:

 

0

The output will zero as 1/5 and its .05 but conversion to int make it zero

Above is prototype of template class so assume passing an int and returning an int

 

 

 

Question No: 27      ( Marks: 3 )

 

Identify the errors in the following member operator function and also correct them.

 

math * operator(math m);

math * operator (math m)

{

     math temp;

     temp.number= number * number;

     return number;

   

}

 

Answer:

 

The errors are in the arguments of the member operation function and also in the body of operator member function.

Correct function should be

math *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

 

 

Question No: 28      ( Marks: 3 )

 

What are the limitations of the friendship relation between classes?

 

Answer:

friendship relation between classes is a one way relation that is if one class declare friend another class then the another class is the friend of first class but not the first class if the friend of another class.

 

 

Question No: 29      ( Marks: 3 )

 

Define static variable. Also explain life time of static variable?

 

Answer:

When you declare a static variable (native data type or object) inside a function, it is created and initialized only once during the lifetime of the program.

 

Question No: 30      ( Marks: 5 )

 

What is difference between Unary and binary operators and how they can be overloaded?

 

Answer:

Unary operator takes one argument.

a ++ is an example of unary operator

 

Binary take two operators

+,-,* are example of binary operators

Overloaded binary operator may return any type

 

Here is general syntax of overloading

Return-type operator symbol (parameters);

 

Operator is keyword

 

Question No: 31      ( Marks: 5 )

 

What steps we must follow to design good program?

 

Answer 1:

 

Hence to design a program properly, we must:

  • Analyze a problem statement, typically expressed as a word problem.
  • Express its essence, abstractly and with examples.
  • Formulate statements and comments in a precise language.
  • Evaluate and revise the activities in light of checks and tests and
  • Pay attention to detail.

 

Answer 2:

 

Details:  we must check very details of any program. It is very important aspect of any program.  We must pay complete attention to calculation.

We must give attention to logic and its flow should be smooth.

 

Reusable: We must write program in such a way that we can reuse them in other program. Like we define function in such a way that in future if we need any similar kind of function is requires in that case we can easily modify or reuse it.

 

Comments: we write the details of important steps in the form of comments. We should use comments in such a way if any body else wanted to reuse or debug or codes he can easily understand it.

 

 

Code readability: We should use Tab and spaces so codes are easily readable.

 

User interface: we make interface user friendly. Use polite prompts for user while take input.

 

Question No: 32      ( Marks: 5 )

 

Write a program which defines five variables which store the salaries of five employees, using setw and setfill manipulators to display all these salaries in a column.

Note: Display all data with in a particular width and the empty space should be filled with character x

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

 

Answer:

 

#include

#include

main(){

int sal1 =1000;

int sal2 =1500;

int sal3 =20000;

int sal4 =30000;

int sal5 =60000;

cout << setfill (‘x’) << setw (10);

cout<< sal1<

cout << setfill (‘x’) << setw (10);

cout<< sal2<

cout << setfill (‘x’) << setw (10);

cout<< sal3<

cout << setfill (‘x’) << setw (10);

cout<< sal4<

cout << setfill (‘x’) << setw (10);

cout<< sal5<

int i=0;

cin>>i; // to stop the screen to show the output

}

 

http://www.thecyberians.com/vu-forum  

Question No: 33      ( Marks: 5 )

 

Suppose we have the following class.

 

class Matrix

{

private:

int Elements[3][3];

};

 

Write the operator function of stream extraction operator (>>) for this class.

 

Answer:

Element operator >> (Element &element){

cout<

cout<

cout<

}

 

Question No: 34      ( Marks: 5 )

 

 

What is meant by user interface and class interface in C++ ? And what role a class interfaces can play in user interface [Marks 5]

 

Answer:

 

http://www.thecyberians.com/vu-forum

 

Question No: 35      ( Marks: 5 )

 

Write the general syntax of a class that has one function as a friend of a class along with definition of friend function.

 

Answer:

class frinedclass{

public:

friend int compute(exforsys e1)

};

Int compute(exforsys e1)
{
//Friend Function Definition which has access to private data
return int(e1.a+e2.b)-5;
}

 

Question No: 36      ( Marks: 5 )

 

What are the advantages and disadvantages of using templates?

 

Answer:

Many things can be possible without using templates but it does offer several clear advantages not offered by any other techniques:

 

Advantages:

  • Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.
  • Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.
  • Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.
  • It helps in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.

 

Disadvantages:

On the other hand, if misused

  • Templates can make code difficult to read and follow depending upon coding style.
  • They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.
  • Then, there are times, when templates can “excellently” produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.

 

Question No: 37      ( Marks: 5 )

 

Suppose a program has a math class having only one data member number.

Write the declaration and definition of operator function to overload + operator for the statements of main function.

         math obj1, obj2;

         obj2= 10 + obj1  ; 

 

Answer:

#include

math

{

mth operator + (obj1,int x)

{

 

number temp;

temp=obj1.number+x;

return temp.number;

}

}

 

Question No: 38      ( Marks: 5 )

 

Write a program which defines three variables of type double which store three different values including decimal points, using setprecision manipulators to print all these values with different number of digits after the decimal number.

 

Answer:

 

#include

#include

main () {

double a = 12.12345;

double b = 13.123456;

double c = 14.1234567;

cout << setprecision (5) << a << endl;

cout << setprecision (2) << a << endl;

cout << setprecision (3) << a << endl;

}

 

Question No: 39      ( Marks: 5 )

 

Let we have a class,

class String

{

private:

char  buf[25];

};

Write code for assignment (=) operator function which assign one String object to other object. Your code should also avoid self assignment

 

Answer:

void String::operator = ( const String &other )

{ int length ;

length = other.length();

delete buf;

buf = new char [length + 1];

strcpy( buf, other.buf ); }

 

Question No: 40      ( Marks: 5 )

 

Read the given below code and explain what task is being performed by this function

Matrix :: Matrix ( int row , int col )

{

    numRows = row ;

    numCols = col ;

    elements = new ( double * ) [ numRows ] ;

    for ( int  i = 0 ; i < numRows ; i ++ )

{

        elements [ i ] = new double [ numCols ] ;

        for ( int j = 0 ; j < numCols ; j ++ )

                elements [ i ] [ j ] = 0.0 ;

     }

}

Hint : This function belong to a matrix class, having

Number of Rows = numRows

Number of Columns = numCols

 

Answer:

In the above mentioned code, first of all programmer call the constructor who have two parameters for the number of rows & columns in the matrix. Then this constructor also dynamically allocates the memory for the elements of the matrix & also initializes the value of the all elements of matrix with 0.0

 

 

VU CS201 -Introducing To Programming MIDTERM Solved/Unsolved Papers Spring 2010

Education In Pakistan © 2016