Home / CS201

CS201

CS201 Introduction to Programming Assignment No 3 Due Date: 16th Jan. 2017

CS201 Introduction to Programming Assignment No 3 Due Date: 16th Jan. 2017

Instructions

Please read the following instructions carefully before submitting assignment:

It should be clear that your assignment will not get any credit if:

 

  • Assignment is submitted after due date.
  • Submitted assignment does not open or file is corrupt.
  • Assignment is copied (From internet/students).
  • Assignment is implemented other than using structure method.

Software allowed to develop Assignment

  • Dev C++

 

Objectives:

To enable students to write, compile and execute a program in Dev C++. Moreover to familiarize students with  the concepts of:

  • Structure
  • User define functions
  • String functions
  • Formatting

 

Assignment Submission Instructions

You have to submit only.cpp file on the Assignments interface of CS201 at VULMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.

XYZ University needs a system for student’s courses and their semester details. A data entry operator needs to enter student’s data in to the system, and then the system will show the report of student semester information. Following data will be used by the system.

 

  • Course code
  • Course name
  • Semester

 

The system will allow entering data for five courses at a time and will input course code along with course name, later the system will format the report in a more readable form.

 

Your task is to:

Write a C++ program to implement the above mentioned interface

 

  • Your program should provide the user with options to enter data for courses along with course code. e.g.

Enter course name along with course code: CS201 Introduction to Programming

  • After taking course information from user your program should prompt user to enter semester information.

e.g. Enter Semester: Fall 2016

 

  • The program should input data for five courses and semester.
  • Use structure name “course” in your program.
  • Use separate functions for taking user inputs, displaying and formatting outputs.

e.g. InputCourses(),DisplayHeader(),DisplayCourses(),FormatCourse(—) etc.

 

Note: Use structure to implement above program, no credit will be awarded if structure is not used.

Sample output:

The deadline to submit your assignment solution is 16th January, 2017. Your assignment must be submitted within the due date through VULMS. No assignment will be accepted through email after the due date.

 

Share with Friends

CS201 Final Term Past Papers Mega Collection

CS201 Final Term Past Papers Mega Collection

Download Files

CS201 All finalterm papers mega file Vuhelpcorner.com (2)

CS201 All finalterm papers mega file Vuhelpcorner.com (2)

CS201 All finalterm papers mega file Vuhelpcorner.com (3)

CS201 All finalterm papers mega file Vuhelpcorner.com (3)

CS201 All finalterm papers mega file Vuhelpcorner.com (4)

CS201 All finalterm papers mega file Vuhelpcorner.com (4)

CS201 All finalterm papers mega file Vuhelpcorner.com (5)

CS201 All finalterm papers mega file Vuhelpcorner.com (5)

CS201 All finalterm papers mega file Vuhelpcorner.com (6)

CS201 All finalterm papers mega file Vuhelpcorner.com (6)

CS201 All finalterm papers mega file Vuhelpcorner.com (7)

CS201 All finalterm papers mega file Vuhelpcorner.com (7)

CS201 All finalterm papers mega file Vuhelpcorner.com (8)

CS201 All finalterm papers mega file Vuhelpcorner.com (8)

CS201 All finalterm papers mega file Vuhelpcorner.com (9)

CS201 All finalterm papers mega file Vuhelpcorner.com (9)

CS201 All finalterm papers mega file Vuhelpcorner.com (10)

CS201 All finalterm papers mega file Vuhelpcorner.com (10)

CS201 All finalterm papers mega file Vuhelpcorner.com (11)

CS201 All finalterm papers mega file Vuhelpcorner.com (11)

CS201 All finalterm papers mega file Vuhelpcorner.com (12)

CS201 All finalterm papers mega file Vuhelpcorner.com (12)

CS201 All finalterm papers mega file Vuhelpcorner.com (13)

CS201 All finalterm papers mega file Vuhelpcorner.com (13)

CS201 All finalterm papers mega file Vuhelpcorner.com (14)

CS201 All finalterm papers mega file Vuhelpcorner.com (14)

CS201 All finalterm papers mega file Vuhelpcorner.com (15)

CS201 All finalterm papers mega file Vuhelpcorner.com (15)

CS201 All finalterm papers mega file Vuhelpcorner.com (16)

CS201 All finalterm papers mega file Vuhelpcorner.com (16)

CS201 All finalterm papers mega file Vuhelpcorner.com (17)

CS201 All finalterm papers mega file Vuhelpcorner.com (18)

CS201 All finalterm papers mega file Vuhelpcorner.com (19)

CS201 All finalterm papers mega file Vuhelpcorner.com (20)

CS201 All finalterm papers mega file Vuhelpcorner.com

CS201 All finalterm papers mega file Vuhelpcorner.com

CS201 All finalterm papers mega file Vuhelpcorner.com

Share with Friends

CS201 Final Term Short Notes From Lesson 23-45

CS201 Final Term Short Notes

 

Download File 

CS201 Final Term Short Notes Lesson 23-45

Joine Us:  http://vuhelpcorner.com/

 

 

#includes:      

The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system provided file, or in quotes if the file is user provided.

Abstract class:          

A class that can only be used as a base class for some other class. A class is abstract if it has at least one pure virtual function.

Aggregate:    

An array or object of a class with no constructors, no private or protected members, no base classes, and no virtual functions. Also see initializer and initialization.

Alias:

Defining an alias for a structure or built-in type allows use of the alias as if it were a new type. In C, the keyword is typedef. It’s a bad idea to alias (have two objects pointing to the same data) unless one of those objects is a temporary variable.

Allocation:

The process of giving memory space to an object. Also see dynamic storage, static storage, and deallocation

Argument:

When calling a function, refers to the actual values passed to the function. Also see parameter.

Array:

An ordered and indexable sequence of values. C++ supports arrays of a single dimension (a vector) or of multiple dimensions.

Assignment:

The process of giving a value to a pre-existing object. Also see copy constructor and initialization.

Base class:

A class that serves as a base for a derived class to inherit members from. Also see inheritance.

Bit:

A bit is the smallest unit of memory that can be manipulated. It has a binary or boolean value of 0 or 1. Within memory bits are combined to form bytes and words. Depending on its type, a variable in a program is stored in some number of bytes.

Joine Us:  http://vuhelpcorner.com/

 

Bool:

C++ keyword used to declare a Boolean data type.

Break:           

C++ keyword used to specify a statement that is used to break out of a for or while loop or out of a switch statement.

Byte:

A byte is a grouping of eight bits which a computer typically groups as one logical unit. It is usually the smallest addressable unit of memory.

Call by reference:

Passing a pointer to an argument to a function. The function can then change the argument value. Also see call by value.

Call by value:

Passing a copy of an argument to a function. The function cannot then change the argument value. C and C++ use call by value argument passing. But also see pointer and reference, also see call by reference.

Case:

A C++ keyword used to denote an individual element of a switch statement.

Char:

A C++ keyword used to declare an object of character type. Often considered the same as a byte, though it is possible to have multi-byte characters.

cin:

In C++ stream I/O, the standard input stream.

Class:

A C++ keyword used to declare the fundamental building block of C++ programs. A class has a tag, members, access control mechanisms, and so on.

Class layout:

The way in which data class members are arranged in a class object

Class member:

A constituent member of a class, such as a data declaration, a function, or a nested class.

Class template:

A template used for generating class types.

Comments:

C++ has C-style comments delimited with /* and */, and new C++-style line-oriented comments starting with //.

Compiler:

A software tool that converts a language such as C++ into a different form, typically assembly language.

Const:

A C++ keyword used to declare an object as constant or used to declare a constant parameter.

Constructor:

A function called when a class object comes into scope. The constructor is used to initialize the object. See allocation, copy constructor, and destructor.

Continue:

C++ keyword used with for and while statements to continue the iteration at the top of the loop.

Copy constructor:

A special type of constructor that is called when an object is copied.

cout:

In C++ stream I/O, the standard output stream.

Data structure:

The term data structure refers to the way data is organized for use within a program. Correct organization of data can lead to simpler and more efficient algorithms. Common data structures are linked-lists, stacks, queues and trees.

Debugger:

A tool for stepping through the execution of a program, examining variables, setting breakpoints, and so on.

Declaration:

A C++ entity that introduces one or more names into a program. declaration specifies to the compiler the types of all the elements of an identifier; “this function or this piece of data exists somewhere else, and here is what it should look like.” See also definition for definition.

Default argument:

An optional argument to a function. A value specified in the function declaration is used if the argument is not given.

Definition:

Instantiates an identifier, allocating its memory. A declaration can also be a definition. You can declare data or a function in many difference places, but can define it only once.

Delete operator:

C++ keyword and operator used to delete dynamic storage.

Derived class:

A class that inherits members from a base class.

Destructor:

A function called when a class object goes out of scope. It cleans up the object, freeing resources like dynamic storage.

Do:

A C/C++ reserved word that allows construction of an iterative loop. The statements in the body always execute at least once. Also see while.

Double:

A fundamental data type in C and C++. Double variables are used to store floating-point values. They offer greater precision and can store larger numbers than floats.

Dynamic memory allocation:

The process of dynamically creating objects in the heap or free store during program runtime. Statically created objects, created by the compiler, are put on the program stack.

Else:

C++ keyword, part of the if statement, that allows conditional execution of code.

endl:

The standard C++ library provides a set of manipulator functions that can modify the state of iostream objects. endl writes a new line to output.

Explicit:

A C++ keyword used in the declaration of constructors to indicate that conversion of an initializer should not take place.

Expression:

A combination of constants, variables, and operators used to produce a value of some type.

Expression statement:

A statement that is an expression, such as a function call or assignment.

Float:

A C++ keyword used to declare a floating point type.

For:

A C++ keyword used to specify an iteration or looping statement.

Friend:

A type of declaration used within a class to grant other classes or functions access to that class.

Function:

A C++ entity that is a sequence of statements. It has its own scope, accepts a set of argument values, and returns a value on completion.

Function overloading:

The capability of having several routines in a program with the same name. The different functions are distinguished by their parameter types, return value types, or both; the compiler automatically selects the correct version, based on parameter types and return types.

Garbage collection:

A way of automatically managing dynamic storage such that explicit cleanup of storage is not required. C++ does not have garbage collection.

Global variable:

A variable that is accessible throughout the whole program, whose lifetime is that of the program.

goto:

C++ keyword, used to transfer control within a C++ function.

Header file:

A file containing class declarations, preprocessor directives, and so on, and included in a translation unit. It is expanded by the preprocessor.

Heap:

A pool of memory used for dynamic memory allocation. Blocks of memory from this area are allocated for program use during execution using the new operator in C++ and the malloc function in C.

If:

C++ keyword used in conditional statements, that allows conditional execution of code.

Inheritance:

The process whereby a derived class inherits members from a base class. A derived class will also add its own members to those of the base class.

Inline:

C++ keyword used to declare an inline function.

Inline function:

A function that can be expanded by a compiler at the point of call, thereby saving the overhead time required to call the function. Best for recursive and loop calls. Provides type safety and side-effects protection not afforded by #define.

int:

The C/C++ keyword int is used to declare an integer variable.

Interface:

C++ separates its interface from its implementation. The interface in C++ is the class definition of an object, and its methods. The interface doesn’t specify how the methods work; this is done in the class implementation. Only the interface need be compiled at compile time. The implementation can be linked (or even written) at any time. You can compile a system to make sure it all fits together without writing its implementation.

Keyword:

A reserved identifier in C++, used to denote data types, statements of the language, and so on. Keywords are reserved words that serve a special purpose within a programming language. They may declare the type or properties of an object, or allow creation of program structure such as branches or loops. They may not by used as identifiers or object (variable) names.

Label:

A name that is the target of a goto statement.

Linker:

A program that combines object files and library code to produce an executable program.

Macro:

A preprocessor feature that supports parameter substitution and expansion of commonly-used code sequences. Also see inline function.

Main:

Every C or C++ program must have one and only one main function. Program execution begins with the first statement in main

malloc:

malloc is used to dynamically allocate memory in the C programming language. malloc returns a pointer of type void to a memory buffer of a requested size, or null if it fails. It may also be used in C++, although new is preferred.

Manipulators:

Special iostream functions permitting specific i/o operations while in the middle of stream insertion or extraction. These functions switch appropriate iostream format flags.

Memory leak:

In C/C++, it is possible to allocate space for objects (variables) dynamically during program execution. After finishing use of a dynamically allocated object, it is necessary to explicitly release the memory consumed by the object, particularly before pointers to the object go out of scope. Failure to do so results in a memory leak. Long running programs or those that allocate memory extensively can consume or occupy enough of a computers memory to seriously hinder performance when memory leaks are present.

Name mangling:

A way of encoding an external name representing a function so as to be able to distinguish the types of its parameters.

New operator:

C++ keyword and operator used to allocate dynamic storage.

Null pointer:

A pointer value that evaluates to zero.

Object:

In C++, often refers to an instance of a class. Also more loosely refers to any named declaration of a variable or other entity that involves storage.

OOP:

Acronym for object-oriented programming.

Operator:

A builtin operation of the C++ language, like ddition, or an overloaded operator corresponding to a member function of a class.

Operator overloading:

To treat a C++ operator like << as a function and overload it for particular parameter types. The assignment of more than one function to a particular operator, with the implication that the operation performed will vary depending on the data type (operands) involved.

Overload:

To specify more than one function of the same name, but with varying numbers and types of parameters.

Parameter:

Refers to the variables passed into a function.

Pointer:

An address of an object.

Joine Us:  http://vuhelpcorner.com/

 

Pointer to data member:

A pointer that points at a data member of a class.

Pointer to function:

An address of a function or a member function.

Preprocessing:

A stage of compilation processing that occurs before the compiler proper is invoked. Preprocessing handles macro expansion among other things. In C++ use of const and inline functions makes preprocessing less important.

Preprocessor:

A device or routine that performs preliminary operations on input before passing it on for further processing. See also preprocessing.

Private:

A C++ keyword used to specify that a class member can only be accessed from member functions and friends of the class. See access control, protected, and public.

Protected:

A C++ keyword used to specify that a class member can only be accessed by member functions and friends of its own class and by member functions and friends of classes derived from this class. See also private and public.

Prototype:

A prototype is a function declaration. Function declarations are generally placed in header files.

Public:

A C++ keyword used to specify that class members are accessible from any (non-member) function.

Queue:

A queue is a data structure that supports FIFO, first in first out processing. Objects will be processed in the order they enter a queue. Conceptualize a queue as a water pipe. The first water to flow in will be the first water to flow out of the other end.

Reference:

Another name for an object. Access to an object via a reference is like manipulating the object itself. References are typically implemented as pointers in the underlying generated code.

Register:

C++ keyword used as a hint to the compiler that a particular local variable should be placed in a machine register.

Reserved word:

Keywords that serve a special purpose within a programming language. They may declare the type or properties of an object, or allow creation of program structure such as branches or loops. They may not by used as identifiers or object (variable) names.

Return:

C++ keyword used for returning values from a function.

Return value:

The value returned from a function.

Scope:

The region of a program where a name has visibility. Generally, the domain of objects and calls within which an object can exist. A struct name or enum in an inner scope can obscure a global of the same name in an outer scope. In C++, enum constants embedded in a struct def have the same scope as that struct’s fields.

Short:

A C++ fundamental type used to declare small integers.

Signed:

C++ keyword used to indicate a signed data type.

String:

A set of consecutive characters treated by a computer as a single item. In C, strings are represented in null , ‘\0’, terminated character arrays. In C++, it is common to use the string class, which is part of the standard C++ library to hold and manipulate strings.

Switch:

C++ keyword denoting a statement type, used to dispatch to one of several sequences of statements based on the value of an expression.

  • Notes

 

  • Exam Results

 

  • Papers

 

  • Vectors

 

  • Computers

 

  • Compiler

 

  • Form

 

  • Access Control

 

  • Aggregates

 

  • Allocations
  • Notes

 

  • Exam Results

 

  • Papers

 

  • Vectors

 

  • Computers

 

  • Compiler

 

  • Form

 

  • Access Control

 

  • Aggregates

Template:

A parameterized type. A template can accept type parameters that are used to customize the resulting type.

Template argument: 

An actual value or type given to a template to form a template class.

Template class:

A combination of a template with a template argument list via the process of template instantiation.

This:

C++ keyword used in a member function to point at the object currently being operated on.

Union:

A structure somewhat like a class or struct, except that individual union members share the same memory.

Virtual function:

A member function whose interpretation when called depends on the type of the object for which it is called; a function for an object of a derived class will override a function of its base class.

While:

C++ keyword used to declare an iteration statement. It means ‘do until the condition is true’.

 

Share with Friends

CS201-Mid Term PastPapers

CS201 – Introduction to Programmming

Solved Subjective Questions

From spring 2010 Mid term Papers

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 postincrement

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

Page. No. 2/15

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 ;

Come & Join Us at www.vustudents.net

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.

Page. No. 3/15

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<iostream.h>

main () {

Come & Join Us at www.vustudents.net

int matrix [3][3];

int inivalue = 0;

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

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

{ matrix[a]= inivalue;

cout<<matrix[a]<<endl;}}

}

Page. No. 4/15

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 <class T, class U>

T func (T a, U b) {

return (a<b?a:b);

}

calling

int i;

double x;

x = func<int,double> (j,l);

Come & Join Us at www.vustudents.net

Page. No. 5/15

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<<d1.day<<”-”<<d1.month<<”-”<<d1.year;

}

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.

Page. No. 6/15

\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 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

Come & Join Us at www.vustudents.net

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:

Page. No. 7/15

11

1

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Answer:

1

23

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;

Come & Join Us at www.vustudents.net

m1 = m2;

3) Matrix m1 = m2;

Page. No. 8/15

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;

Page. No. 9/15

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:

Come & Join Us at www.vustudents.net

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

Page. No. 10/15

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

Come & Join Us at www.vustudents.net

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

Page. No. 11/15

Output should be displayed as given below:

xxxxxx1000

xxxxxx1500

xxxxx20000

xxxxx30000

xxxxx60000

Answer:

#include <iostream.h>

#include <iomanip.h>

main(){

int sal1 =1000;

int sal2 =1500;

int sal3 =20000;

int sal4 =30000;

int sal5 =60000;

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

cout<< sal1<<endl;

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

cout<< sal2<<endl;

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

cout<< sal3<<endl;

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

cout<< sal4<<endl;

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

cout<< sal5<<endl;

int i=0;

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

}

Question No: 33 ( Marks: 5 )

Suppose we have the following class.

class Matrix

{

private:

Page. No. 12/15

Come & Join Us at www.vustudents.net

int Elements[3][3];

};

Write the operator function of stream extraction operator (>>)

for this class.

Answer:

Element operator >> (Element &element){

cout<<element[0][0]<<element[0][1]<<element[0][2];

cout<<element[1][0]<<element[1][1]<<element[1][2];

cout<<element[2][0]<<element[2][1]<<element[2][2];

}

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:

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?

Page. No. 13/15

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

Come & Join Us at www.vustudents.net

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)

Page. No. 14/15

{

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;

Come & Join Us at www.vustudents.net

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: 95 ( 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;

Page. No. 15/15

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

 

No.1

CS201 Subjective:

Question: What is the use of reference data type?

Answer: A reference data type is a variable that can contain an address. The reference

data types in Java are arrays, classes and interfaces. You’ll hear often say that Java does

not have pointers. Yet, you could consider a reference data type to be a pointer

Question: What are the main types of operators in terms of number of arguments

they take?

Answer: The difference is in the number of arguments used by the function. In the case

of binary operator overloading, when the function is a member function then the

number of arguments used by the operator member function is one. When the function

defined for the binary operator overloading is a friend function, then it uses two

arguments.

Question: What is the “this” pointer? Give an example of its use

Answer: In a C++ program, if you create object A of class X, you can then obtain the

address of A by using the “this” pointer. The address is available as a local variable in the

non-static member functions of X, and its type is const X*. The “this” pointer works

because C++ creates instances of its data members, and it keeps one copy of each

member function.

Question: What are manipulators? Give one example.

Answer:Manipulators are operators used in C++ for formatting output. The data is

manipulated by the programmer‟s choice of displayed.

Endl manipulator: This manipulator has the same functionality as the „\n‟ newline

character.

Question:If the requested memory is not available in the system then what does

calloc/malloc and new operator return?

Answer: malloc returns a void pointer to the allocated space or NULL if there is

insufficient memory available. To return a pointer to a type other than void, use a type

cast on the return value. The storage space pointed to by the return value is guaranteed

to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zerolength

item in the heap and returns a valid pointer to that item.

By default, malloc does not call the new handler routine on failure to allocate memory.

You can override this default behavior so that, when malloc fails to allocate memory,

malloc calls the new handler routine in the same way that the new operator does when it

fails for the same reason.

Question: If we want to send the data by reference and don‟t want that original

data should be affected then what can we do to prevent any change?

Answer: Call by Reference shall be used in that case.

Question: Write down the disadvantages of the templates.

Answer: The disadvantages of templates are:

  • Templates can make code difficult to read and follow depending upon coding style.

No.2

  • 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: The following code segment has errors. Locate as many as you can and

explain briefly.

Answer:

class Circle // no need to enter colon here ,

{

private : //colon missing

double centerX;

double centerY;

double radius;

public: //colon missing

void setCenter(double, double);

void setRadius(int);

};//semi colon missing

Question ( 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 <iostream>

#include <iomanip>

int main ()

{

double x1 = 12345624.72345

double x2 = 987654.12345

double x3 = 1985.23456

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

cout << setprecision (4) << x2 << endl;

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

return 0;

}

Question ( 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;

}

No.3

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 *operator (math *m)

{

math temp;

temp = m;

temp.number= number * number;

return temp.number;

}

Question ( Marks: 10 )

Write a program which consists of two classes, Date and Person.

Date class should contain three data members day, month, year and setter and getter

function for these data members. Date class should also contain showdate() member

function to display date.

Person class should contain three data members Name, Address, and Bday, where

Name and Address are char pointer while Bday(Date of birth) is of type Date, Person

class should further contain two member functions Display() and setdate().

In main program Create an object of Class person and call the member functions with it.

Answer:

#include <stdio.h>

#include <iostream>

#include <cstring>

using namespace std;

class Date

{

public:

int day;

int month;

int year;

public:

Date()

{

day=0;

month=0;

year=0;

}

void setDay(int);

void setMonth (int);

void setYear(int);

int getDay();

int getMonth();

int getYear();

void showDate();

};

No.4

void Date: :setDay(int d)

{

if{d<1 | | d>31)

cout<<“Invalid month Renter it”;

cin>>d;

}

day=d;

}

void Date: :setMonth (int m)

{

if(m<1 | | m>12)

{

cout<<“Invalid month Renter it”;

cin>>m;

}

month=m;

}

void Date: :setYear (int y)

{

year=y;

int Date: :getDay()

{

return day;

}

int Date: :getMonth()

{

return month:

}

int Date: :getYear()

{

return year;

}

void Date: :showDate()

{

cout<<day<<“-“<<month<<“-“<<year<<end1;

}

Class Person

{

public:

char *Name;

char *Address

Date Bday;

public:

Student()

{

Name=new char[20];

No.5

Address=new char[10];

cin.getline(Name,20);

cout<<“Enter Address:”;

cin.getline(Address,10);

}

void setDate()

{

cout<<“Enter Day:”;

cin>>Ad_date.day;

cout<<“Enter month:”;

cin>>Ad_date.month;

cout<<“Enter Year:”;

cin>>Ad_date.year;

}

void Display()

{

cout<<“Name: “<<end1;

cout<<“Address: “<<Address<<end1;

cout<<“Date of Birth: “;

Ad-date.showDate();

}

};

void main()

{

Person object;

object.setDate();

object.Display();

system(“pause”);

}

Question ( Marks: 10 )

Write a C++ program that contains a class „myClass‟ having two data members of type

int.

The class must have

  • A default constructor which must initialize all the data members to their meaningful

values.

  • A destructor with no implementation.
  • Setter member functions to set all data members of class
  • Getter member functions to get all data members of class

In main function of the program

  1. Prompt the user to enter the number of objects to be created.
  2. Dynamically allocate memory to objects according to the size entered by user.
  3. De-allocate memory that was allocated to objects

Answer:

#include <stdio.h>

#include <iostream>

No.6

#include <cstring>

using namespace std;

class myclass

{

public:

int a;

int b;

int *iptr, *sptr;

construct{int,int.int}

void seta(int);

void setb(int);

void setc(int);

int geta();

int getb();

int getc();

};

void Person: :seta(int aa)

{

a=aa;

}

void Person: :setb (int bb)

{

b=bb;

}

void Person: :setc (int cc)

{

c=cc;

}

main()

{

int num;

cout<<“Enter the number of objects to be created”;

cin>>num;

for (int i =1;i==num;i++)

{

Person i_

}

}

Question No: 31 ( Marks: 1 )

How do we provide the default values of function parameters?

Answer:

No.7

Question ( Marks: 1 )

Why do java consider pointer as dangerous

Answer: JAVA, describe pointers as dangerous . if we assign a memory through a

pointer where the pointer is destroyed, the memory remains allocated and is wasted. To

address these things, there are only references in JAVA instead of pointers. JAVA gives

the concept of garbage collection with the use of references. Due to this garbage

collection, we are free from the headache of de- allocating the memory. We allocate and

use the memory. When it is no longer in use, JAVA automatically deletes (frees) it

through garbage collection

Question ( Marks: 2 )

What is memory leak?

Answer: There is a requirement that if the constructor of a class allocates the memory,

it is necessary to write a destructor of that class. We have to provide a destructor for that

class, so that when that object ceases to exist, the memory allocated by the constructor,

is returned to the free store. It is critically important. Otherwise, when the object is

destroyed, there will be an unreferenced block of memory. It cannot be used by our

program or by any other program. It‟s a memory leak that should be avoided.

Question ( Marks: 2 )

What does optimization the of code means?

Answer: Optimization is the process of transforming a piece of code to make more

efficient without changing its output or side-effects. The only difference vuzs visible to the

code‟s user should be that it runs faster and/or consumes less memory.

Question ( Marks: 3 )

What is the difference between structure and class?

Answer: The ONLY DIFFERENCES between classes and structures are

1) classes DEFAULT to having private members. Structures DEFAULT to having public

members. These defaults can be changed so classes can be made to work like structures

and vice versa.

2) classes DEFAULT to inheriting privately from base classes. Structures DEFAULT to

inheriting public from base classes. These defaults can be changed so classes can be

made to work like structures and vice versa.

Question ( Marks: 3 )

See the following code segment.

template

class myclass {

private:

T x;

public:

myclass (T a) {

x = a;

}

};

Write the main function which creates two objects of class for int and double data types.

Answer:

Question ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Answer:

No.8

Yes, function can be defined in both given ways, as

in function no.1 , function is calling the variable by its value (which is default function

call method).In the second function , function call is done by using call by reference

methos.

Question ( Marks: 5 )

Write a program using getline() member function to inputs a string up to delimiter

character comma (,) and then display the string on the screen.

Answer:

Question ( Marks: 5 )

Do you think that friend functions violate encapsulation? Justify your answer.

Answer: The friend functions of a class have access to the private data members of

class. Despite being a good thing, there is possibility of vulnerability. We are opening our

thoughts, inside view for somebody else. Without having 100% trust, it will be risky to

make our thoughts and feelings public. We want that our private data is accessible to

someone outside, not public for everybody. Otherwise, the data encapsulation and datahiding

concept will be violated. We keep the data members private and declare some

specific functions that are not member of the class but friend of the class. As friends,

they have access to the inside data structure of the class despite not being members.

Question ( Marks: 10 )

Write a simple program using the get() member function of cin object reading a text of

30 characters from the keyboard, store them in an array and then using put() member

function of cout object to display them on the screen.

Answer:

Question ( Marks: 10 )

Write a small program which defines two user-defined manipulators named octal and

hexadecimal. These manipulators should display the decimal numbers into octal and

hexadecimal.

In the main function, input a decimal number from the user and then display this

decimal number into octal and hexadecimal using user-define manipulators named octal

and hexadecimal.

Answer:

Question: What are User defined Manipulators?

Answer: Parameterized manipulators require one or more arguments. setfill (near the

bottom of the iomanip.h header file) is an example of a parameterized manipulator. You

can create your own parameterized manipulators and your own simple manipulators.

Question: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.

No.9

Question: 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: 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: What are the limitations of the friendship relation between classes?

Answer: Class can declare a friend class from inside and someone from outside the

class cannot declare itself friend of a class.

Question: 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: 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:

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: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 ] ;

No.10

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

Question: What is an assignment operator?

Answer :Assignment Operator

At first, we ascertain whether there is need of an assignment operator or not? It is

needed when we are going to assign one object to the other, that means when we want

to have expression like a = b. C++ provides a default assignment operator. This

operator does a member-wise assignment.

Question: What is drawback of writing the definitions of all the functions before

main function?

Answer

Question ( Marks: 1 )

How do we provide the default values of function parameters?

Answer

Question ( Marks: 2 )

What is difference between endl and \n?

Answer

Question ( Marks: 2 )

When does an object get destroyed?

Answer

Question ( 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;

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question ( Marks: 3 )

Why stream insertion and stream extraction operators cannot be overloaded as member

functions?

No.11

Question No: 38( Marks: 5 )

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

Question No: 39( Marks: 5 )

What steps we must follow to design good program?

Question No: 40( Marks: 10 )

Write the program that inputs an octal number from the user and then display the

entered octal number into hexadecimal number using manipulators (parameter-less,

parameterized) and member function of input/output streams.

Question No: 41( Marks: 10 )

Develop a class Vector having two data members; x and y.

The class should also provide the following Overloaded operator capabilities.

  1. a) Overload the addition operator(+) to add two Vectors
  2. b) Overload the assignment operator(=) to assign Resultant Vector
  3. c) Write function Display() to display x, y coordinates

Note:Addition of vector Let suppose there are two vectors A and B with their x, y

coordinates.

Question: How do we provide the default values of function parameters?

Answer: The default value of a parameter is provided inside the function prototype or

function definition. For example, we could declare the default function arguments for a

function while declaring or defining it.

Question No: 27 ( Marks: 2 )

What is the difference between switch statement and if statement.

Question No: 28 ( Marks: 2 )

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

Question No: 29 ( Marks: 2 )

How the data members of a class are initialized with meaningful values?

Question No: 30 ( Marks: 2 )

Can we overload new and delete operators?

Question No: 31 ( 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;

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question No: 32 ( Marks: 3 )

No.12

What is the keyword „this‟ and what are the uses of „this‟ pointer?

Question No: 33 ( 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?

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

Question No: 34 ( 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.

Question No: 35 ( Marks: 5 )

Write down the disadvantages of the templates.

Question No: 36 ( 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 <stdio.h>

#include <iostream>

#include <cstring>

using namespace std;

class myclass

{

public:

int a;

int b;

int *iptr, *sptr;

construct{int,int.int}

void seta(int);

void setb(int);

void setc(int);

int geta();

int getb();

int getc();

};

void Person: :seta(int aa)

{

a=aa;

No.13

}

void Person: :setb (int bb)

{

b=bb;

}

void Person: :setc (int cc)

{

c=cc;

}

main()

{

int num;

cout<<“Enter the number of objects to be created”;

cin>>num;

for (int i =1;i==num;i++)

{

Person i_

}

}

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

No.14

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: 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]= 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 )

No.15

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.

No.16

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;

No.17

}

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

No.18

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 )

No.19

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.

No.20

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);

No.21

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

}

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:

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)

};

Page No.22

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

No.23

math

{

mth operator + (obj1,int x)

{

number temp;

temp=obj1.number+x;

return temp.number;

}

}

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 ++ )

{

No.24

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

Question No: 27 ( 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

Question No: 28 ( 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

Question No: 29 ( Marks: 2 )

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

Answer

Question No: 30 ( 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

Question No: 33 ( 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?

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

Question No: 34 ( Marks: 5 )

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

Question No: 35 ( Marks: 5 )

Suppose we have the following class.

class Matrix

{

No.25

private:

int Elements[3][3];

};

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

Question No: 36 ( Marks: 5 )

What is meant by user interface and class interface in C++ ? And what role a class

interface can play in user interface [Marks 5]

Answer

Question: What are the advantages of STL ?

Answer: Advantages of (STL)

  • Templates are easier to write then 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 can be easier to understand, since they can provide a straightforward

way of abstraction type information.

  • 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 help in utilizing compiler optimizations to the extreme.

Question: what is meant by default constructor

Answer:

Question : what is conversion constructor

Answer:

Question: Y we keep utility functions as private in class?

Answer

Question: Write a program using get() function of cin n puts function of cout n take 30

array characters from keyboard n display on screen using these functions?

Question: write a program define a line (-) operator n manipulate it

For example the output of program should beVirtual——–university

2nd: write a program to overload a unary operator(++)n make a date class. Day, year,

month n lastday(last day of month) should be its data members, operator as a function

member,n display function to display date.

Now increment the day by using operator function n month should also b incremented

after 30 or 31 days,n year should also incremented after the 12 month

Question:

What is self assignment mean, why should it be avoided? [3 marks]

Question:

Is it possible to overload operators for primitive data type? Justify [3 marks]

CS 201 Solved Subjective by Muhammad Ishfaq Page No.26

Question:

What are the limitations of the friendship relations between classes?[3 marks]

Answer

Question:

C and C++ are free format languages. What does mean by free format? [2 marks]

Answer

Question:

What are the two types of conversions for user defined data type? [1 marks]

Answer

Question:

Describe the three important constructs to solve a given problem. [5 marks]

Answer

Question:

How can we increase the size of dynamic allocated memory in C? [5 marks]

Answer

Question:

Does the following statement create new variable? If not, why? [1 marks]

int &ref = val

Answer

Question: [10 marks]

Write a program which contains a class Account. The class should contains three data

members Name, AcNo. and Balance. The class shouls further contains two constructors

i.e. default constructor and parameterized constructor. Overload the stream insertion

operator (<<) for this class.

Answer

Question: [10 marks]

Write a program that reads a number that says how many integer numbers to be stored

in an array. Create an array to for the exact size of data and read in that many numbers

into the array.

Answer

Question No: 36 ( Marks: 5 )

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

Come and Join Us at www.vustudents.ning.com

No.27

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

statements of main function.

math obj1, obj2;

obj2= 10 + obj1 ;

Ans:

#include <iostream.h>

math

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

{

mth operator + (obj1,obj2)

mth operator + (obj1,obj2)

}

}

Question No: 36 ( 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 this code the matrix function is defined, it get the number of rows from the

user and create the row of matrix and then get the columns from the user and create the

columns. The New is showing for creating more array space for the data which user

enters. The elements [j] will print the data in matrix form.

Question No: 35 ( Marks: 5 )

Why the first parameter of operator function for << operator must be passed by

reference?

Answer:

Operator<<‘s first parameter must be an ostream passed by reference. Its second

parameter, the IntList that is printed, does not have to be passed as a const-reference

parameter; however it is more efficient to pass it by reference than by value (since that

avoids a call to the copy constructor), and it should not be modified by operator<<, so it

should be a const reference parameter

Question No: 34 ( Marks: 5 )

No.28

What is the difference between function overloading and operator overloading?

Answer:

Difference b/w function overloading and operator overloading is:

In function overloading, the functions have the same name but differ either by the

number of arguments or the type of the arguments.

Operator overloading is to allow the same operator to be bound to more than one

implementation, depending on the types of the operands.

Question No: 33 ( Marks: 3 )

What happens when we use new and delete operator?

Answer:When we use new operator to create objects the memory space is allocated for

the object and then its constructor is called. Similarly, when we use delete operator with

our objects, the destructor is called for the object before deallocating the storage to the

object.

Question No: 32 ( Marks: 3 )

Is it possible to define two functions as given below? Justify your answer.

func(int x, int y)

func(int &x, int &y)

Answer:

No, we cannot define two functions as func(intx, inty) func(int &x, int&y) because it‟s give

an error function not initializing.

Question No: 31 ( Marks: 3 )

Consider the following code segment. What will be the output of the following code

segment?

class class1{

public:

class class2{

public:

class2(){

cout << “Calling default constructor of class2\n” ;

}

};

class1(){

cout << “Calling default constructor of class1\n” ;

}

} ;

main(){

class1::class2 obj1;

class1 obj2 ;

}

Question No: 27 ( Marks: 2 )

Give the general syntax of class template.

Answer:

Syntax of class template:

template <class T>

class class-name()

No.29

{

definition of class

};

Question No: 28 ( Marks: 2 )

What is difference between endl and \n?

Answer:

The difference between endl and \n is that endl is use to start a new line for the next

row

And \n is a new line character.

Question No: 29 ( Marks: 2 )

What is the this pointer? Give an example of its use.

Answer:

This pointer is use to points to the current object in programming.

Question No: 30 ( Marks: 2 )

Identify each of the following as function call, function definition and function

declaration.

int func(int num1, int num2);

Function call:

Function ; Function definition: Integer; Function declaration: Num1

and Num2

int func(int, int);

Function call:

Function ; Function definition: Integer; Function declaration: integers

func(5, 6) ;

Function call:

Function ; Function definition: numbers; Function declaration: 5&6

int func(int num1, int num2){}

Function call:

Function ; Function definition: Integer; Function declaration: Num1 and Num2 from

user

Question No: 27 ( 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?

Question No: 28 ( 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?

No.30

Question No: 29 ( Marks: 2 )

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

Question No: 30 ( Marks: 2 )

Write the C++ code for the declaration of overloaded stream insertion and stream

extraction operator for the object d of type Date.

Question No: 31 ( 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;

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question No: 32 ( Marks: 3 )

If the requested memory is not available in the system then what does calloc/malloc

and new operator return?

Question No: 33 ( 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?

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

Question No: 34 ( Marks: 5 )

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

Question No: 35 ( Marks: 5 )

Suppose we have the following class.

No.31

class Matrix

{

private:

int Elements[3][3];

};

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

Question No: 36 ( Marks: 5 )

What is meant by user interface and class interface in C++ ? And what role a class

interface can play in user interface [Marks 5]

}

2)

void func2(){

static int x = 0 ;

x++;

cout << x << endl ;

}

Question No: 37( Marks: 3 )

Why stream insertion and stream extraction operators cannot be overloaded as member

functions?

Question No: 38( Marks: 5 )

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

overloaded?

Question No: 39( Marks: 5 )

What steps we must follow to design good program?

Question No: 40( Marks: 10 )

Write the program that inputs an octal number from the user and then display the

entered octal number into hexadecimal number using manipulators (parameter-less,

parameterized) and member function of input/output streams.

Question No: 41( Marks: 10 )

Develop a class Vector having two data members; x and y.

The class should also provide the following Overloaded operator capabilities.

  1. a) Overload the addition operator(+) to add two Vectors
  2. b) Overload the assignment operator(=) to assign Resultant Vector
  3. c) Write function Display() to display x, y coordinates

Note:Addition of vector Let suppose there are two vectors A and B with their x, y

coQuestion No: 31( Marks: 1 )

What is drawback of writing the definitions of all the functions before main function?

Question No: 32( Marks: 1 )

How do we provide the default values of function parameters?

Question No: 33( Marks: 2 )

What is difference between endl and \n?

Question No: 34( Marks: 2 )

When does an object get destroyed?

Question No: 35( Marks: 3 )

What is the difference between structure and class?

Question No: 36( Marks: 3 )

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

No.32

1)

void func1(){

int x = 0;

x++;

cout << x << endl;

 

 

__________ __        _ ______

Fantasy.fear@gmail.com

1

MIDTERM EXAMINATION

Fall 2008

CS201- Introduction to Programming (Session – 2)

Time: 60 min

Marks: 38

Question No: 1 ( Marks: 1 ) – Please choose one

What is the output of the following statement?

int i = 2.5; do { cout i * 2; } while (i > 3 && i < 10);

(a) 510

(b) 5

(c) 48

(d) error

Question No: 2 ( Marks: 1 ) – Please choose one

What does !(7) evaluate to in decimal where ! is a NOT operator?

(a) 7

(b) 8

(c) 9

(d) 10

Question No: 3 ( Marks: 1 ) – Please choose one

The condition in while loop may contain logical expression but not relational expression.

(a) True

(b) False

Question No: 4 ( Marks: 1 ) – Please choose one

Searching is easier when an array is already sorted

(a) True

(b) False

Question No: 5 ( Marks: 1 ) – Please choose one

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

(a) _0 – 99

(b) 1 – 99

(c) 0 – 100

(d) 1 – 100

Question No: 6 ( Marks: 1 ) – Please choose one

What will be the value of ‘a’ and ‘b’ after executing the following statements?

a = 3;

b = a++;

(a) 3, 4

__________ __        _ ______

Fantasy.fear@gmail.com

2

(b) 4, 4

(c) 3, 3

(d) 4, 3

Question No: 7 ( Marks: 1 ) – Please choose one

What will be the correct syntax to initialize all elements of two-dimensional array to

value 0?

(a) int arr[2][3] = {0,0} ;

(b) int arr[2][3] = {{0},{0}} ;

(c) int arr[2][3] = {0},{0} ;

(d) int arr[2][3] = {0} ;

Question No: 8 ( Marks: 1 ) – Please choose one

Which of the following function returns the size of a string variable?

(a) _strlength()

(b) _stringlen()

(c) _strlen()

(d) _strLength()

Question No: 9 ( Marks: 1 ) – Please choose one

What will be the range of numbers generated by function rand () % 9?

(a) 0 to 9

(b) _1 to 9

(c) _0 to 8

(d) _1 to 8

Question No: 11 ( Marks: 1 ) – Please choose one

Computer can understand only machine language code.

(c) True

(d) False

Question No: 13 ( Marks: 1 ) – Please choose one

What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

(a) True

(b) False

Detail:-

It mean

5 = 0101

6 = 0110

5 ^ 6 = 0011

If both input is same then the output is 0 and if different then output is 1

Question No: 14 ( Marks: 1 ) – Please choose one

If the file is not properly closed in the program, the program ____________.

__________ __        _ ______

Fantasy.fear@gmail.com

3

(a) _Terminate normally

(b) _Indicate Runtime error

(c) _Indicate Compile time error

(d) _Crashes

Question No: 15 ( Marks: 1 ) – Please choose one

Which of the following header file include string conversion functions?

(a) _string.h

(b) _stdlib.h

(c) _ctype.h

(d) _sconvert.h

Question No: 16 ( Marks: 1 ) – Please choose one

In Program commenting the code liberally is

Solution:-

It need to be self-contained and understandable. Comments should be placed liberally.

The comments should explain the logic, not the mechanics. Try to avoid fancy

programming.

Question No: 17 ( Marks: 1 )

Which header file must be included while handling files?

Solution:-

<fstream.h>

Question No: 18 ( Marks: 1 )

What is meant by C++ statement: const int *ptr = &x;

Solution:-

ptr is a pointer to data of type const int type. And to assign the address of x to

pointer ptr

Question No: 19 ( Marks: 2 )

What is a truth Table?

Solution:-

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 gate. 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.

Question No: 20 ( Marks: 3 )

(1) An array day is declared as: int day[] = {1, 2, 3, 4, 5, 6, 7};

How many elements does array ‘day’ has?

Solution:-

__________ __        _ ______

Fantasy.fear@gmail.com

4

7 elements

(2) If the declaration is changed as: int day[7] = {1, 2, 3, 4, 5, 6, 7};

How many elements does array ‘day’ has?

Solution:-

7 elements

Question No: 21 ( Marks: 5 )

What are similarities and differences between Structures and Unions?

In structures, we have different data members and all of these have their own memory

space. In union, the memory location is same while the first data member is one name for

that memory location. However, the 2nd data member is another name for the same

location and so on. Consider the above union (i.e. intOrChar) that contains an integer and

a character as data members. What will be the size of this union? The answer is the very

simple. The union will be allocated the memory equal to that of the largest size data

member. If the int occupies four bytes on our system and char occupies one byte, the

union intOrChar will occupy four bytes

Question No: 22 ( Marks: 10 )

Write a void function( ); that takes integer numbers from the user and then

displays the sum of odd and even numbers entered by the user. Your program

should terminate if user enters a negative number

Solution:-

#include<iostream.h>

#include<conio.h>

void function(void);

main()

{

function();

getche();

}

void function(void)

{

int n[5];

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

{

cout <<“Enter Element = “;

cin >>n;

}

cout <<“\nODD Inputs are = \n”;

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

{

__________ __        _ ______

Fantasy.fear@gmail.com

5

if(n%2==0)

{

cout <<n<<endl;

}

}

cout <<“\nEVEN Inputs are = \n”;

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

{

if(n%2==1)

{

cout <<n<<endl;

}

}

}

MIDTERM EXAMINATION

Spring 2009

CS201- Introduction to Programming

Question No: 1 ( Marks: 1 ) – Please choose one

The function of cin is

(a) To display message

(b) To read data from keyboard

(c) To display output on the screen

(d) To send data to printer

Question No: 2 ( Marks: 1 ) – Please choose one

In C/C++ language the header file which is used to perform useful task and manipulation

of character data is

(a) cplext.h

(b) ctype.h

(c) stdio.h

(d) delay.h

Question No: 3 ( Marks: 1 ) – Please choose one

How many parameter(s) function getline() takes?

(a) 0

(b) 1

(c) 2

(d) 3

Question No: 4 ( Marks: 1 ) – Please choose one

__________ __        _ ______

Fantasy.fear@gmail.com

6

Word processor is

(a) Operating system

(b) Application software

(c) Device driver

(d) Utility software

Question No: 5 ( Marks: 1 ) – Please choose one

For which values of the integer _value will the following code becomes an infinite

loop?

int number=1;

while (true) {

cout << number;

if (number == 3) break;

number += integer_value; }

(a) any number other than 1 or 2

(b) only 0

(c) only 1

(d) only 2

Question No: 6 ( Marks: 1 ) – Please choose one

Each pass through a loop is called a/an

(a) enumeration

(b) Iteration

(c) culmination

(d) pass through

Question No: 7 ( Marks: 1 ) – Please choose one

A continue statement causes execution to skip to

(a) the return 0; statement

(b) the first statement after the loop

(c) the statements following the continue statement

(d) the next iteration of the loop

Question No: 8 ( Marks: 1 ) – Please choose one

What is the correct syntax to declare an array of size 10 of int data type?

(a) int [10] name ;

(b) name[10] int ;

(c) int name[10] ;

__________ __        _ ______

Fantasy.fear@gmail.com

7

(d) int name[] ;

Question No: 9 ( Marks: 1 ) – Please choose one

Consider the following code segment. What will the following code segment display?

int main(){

int age[10] = {0};

cout << age ;

}

(a) Values of all elements of array

(b) Value of first element of array

(c) Starting address of array

(d) Address of last array element

Question No: 10 ( Marks: 1 ) – Please choose one

What will be the correct syntax to initialize all elements of two-dimensional array to

value 0?

(a) int arr[2][3] = {0,0} ;

(b) int arr[2][3] = {{0},{0}} ;

(c) int arr[2][3] = {0},{0} ;

(d) int arr[2][3] = {0} ;

Question No: 11 ( Marks: 1 ) – Please choose one

How many bytes will the pointer intPtr of type int move in the following statement?

intPtr += 3 ;

(a) 3 bytes

(b) 6 bytes

(c) 12 bytes

(d) 24 bytes

Question No: 12 ( Marks: 1 ) – Please choose one

If there are 2(n+1) elements in an array then what would be the number of iterations

required to search a number using binary search algorithm?

(a) n elements

(b) n+1) elements

(c) 2(n+1) elements

(d) 2(n+1) elements

__________ __        _ ______

Fantasy.fear@gmail.com

8

Question No: 13 ( Marks: 1 ) – Please choose one

Which of the following operator is used to access the value of variable pointed to by a

pointer?

(a) * operator

(b) -> operator

(c) && operator

(d) & operator

Question No: 14 ( Marks: 1 ) – Please choose one

The ________ statement interrupts the flow of control.

(a) switch

(b) continue

(c) goto

(d) break

Question No: 15 ( Marks: 1 ) – Please choose one

Analysis is the ————– step in designing a program

(a) Last

(b) Middle

(c) Post Design

(d) First

Question No: 16 ( Marks: 1 ) – Please choose one

Paying attention to detail in designing a program is _________

(a) Time consuming

(b) Redundant

(c) Necessary

(d) Somewhat Good

Question No: 17 ( Marks: 1 )

Which programming tool is helpful in tracing the logical errors?

Debugger tool is helpful in tracing the logical errors.

Question No: 18 ( Marks: 1 )

Give the syntax of opening file ‘myFile.txt’ with ‘app’ mode using ofstream variable

‘out’.

ofstream outfile;

outfile.open (“myFile.txt “); // Open the file

__________ __        _ ______

Fantasy.fear@gmail.com

9

Question No: 19 ( Marks: 2 )

What is the difference between switch statement and if statement.

In switch statement only one variable can be tested on various condition but using if we

can tested multi variables in single statement.

Question No: 20 ( Marks: 3 )

Identify the errors in the following code segment and give the reason of errors.

main(){

int x = 10

const int *ptr = &x ;

*ptr = 5 ;

}

main()

{

int x = 10;

const int *ptr = &x ;

cout <<ptr;

}

Question No: 21 ( Marks: 5 )

If int array[10]; is an integer array then write the statements which will store values at

Fifth and Ninth location of this array,

cout <<”Enter fifth postion ”;

cin >> array[4];

cout <<”Enter Ninth postion ”;

cin >> array[8];

Question No: 22 ( Marks: 10 )

Write a function BatsmanAvg which calculate the average of a player (Batsman), Call

this function in main program (Function). Take the input of Total Runs made and Total

number of matches played from the user in main function

Q1

When the if statement consists more than one statement then enclosing these

__________ __        _ ______

Fantasy.fear@gmail.com

10

statement in curly braces is,

(a) Not required

(b) Good programming

(c) Relevant

(d) Must

Q2

The while loop becomes infinite,

(a) When the condition is always false

(b) . When the condition is less than zero

(c) When the condition is always true

(d) When the condition contains a logical operator

Q3

Which of the following function(s) is/are included in stdlib.h header file?

(a) double atof(const char *nptr)

(b) int atoi(const char *nptr)

(c) char *strcpy ( char *s1, const char *s2)

(d) 1 and 2 only

Q4

If we want to store a string “abc” in an array str then the size of this array must be

at least,

(a) 2

(b) 3

(c) 4

(d) 5

Q5

No executable code will be generated if error is found during translation of the

program using interpreter.

(a) True

(b) False

Q6

Word processor is

(a) Operating system

(b) Application software

(c) Device driver

(d) Utility software

Q7

Which of the following is the correct syntax to print multiple values or variables in

__________ __        _ ______

Fantasy.fear@gmail.com

11

a single command using cout?

(a) cout << “Hello” + x + “\n”;

(b) cout << “H” << x << “\n”;

(c) cout << “H”, x, “\n”;

(d) cout << (“H” & x & “\n”);

Q8

Which of the following is correct way to initialize a variable x of int type with value

10?

(a) int x ; x = 10 ;

(b) int x = 10 ;

(c) int x, x = 10;

(d) x = 10 ;

Q9

If there is more than one statement in the block of a for loop, which of the

following must be placed at the beginning and the ending of the loop block?

(a) parentheses ( )

(b) braces { }

(c) brackets [ ]

(d) arrows < >

Q10

Name of an array is a constant pointer.

(a) True

(b) False

Q11

How many bytes will the pointer intPtr of type int move in the following

statement?

intPtr += 3 ;

(a) 3 bytes

(b) 6 bytes

(c) 12 bytes

(d) 24 bytes

Q12

What will be the value of ‘a’ and ‘b’ after executing the following

statements?

a = 3;

b = a++;

(a) 3, 4

(b) 4, 4

__________ __        _ ______

Fantasy.fear@gmail.com

12

(c) 3, 3

(d) 4, 3

Q13

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

(a) True

(b) False

Q14

Which of the following is used with bit manipulation?

(a) Signed integer

(b) Un-signed integer

(c) Signed double

(d) Un-signed double

Q15

Which of the follwoing values C++ use to represent true and false?

(a) 1 and 0

(b) 1 and -1

(c) 11 and 00

(d) Any numerical value

Q16

The argument of the isdigit() function is ___________________

(a) a character,

(b) a C-string,

(c) a C++ string class variable

(d) None of the given options.

Q17

Which data type should be used to store the value 50.7 ?

Float

Q18

Why should goto statement be avoided in C/C++?

When structured programming was started, it was urged not to use the

goto statement. Though goto is there in C language but we will not use it in

our programs. It will adopt the structured approach. All of our programs

will consist of sequences, decisions and loop. Because loop provide best

platform to manipulate the data.

Q19

What operator do you use to assign a pointer the address of another variable or

constant? Marks: 2

__________ __        _ ______

Fantasy.fear@gmail.com

13

& sige

i.e.

int i;

int * ptri;

ptri = &i;

Q20

If there are 2n elements in an array then what would be the number of iterations

required to search a number using binary search and linear search? Marks: 3

Q21

Convert the following switch statement into if statements. Marks: 5

switch (operator) {

case ‘+’:

result = op1 + op2;

break;

case ‘-‘:

result = op1 – op2;

break;

case ‘x’:

case ‘*’:

result = op1 * op2;

break;

case ‘/’:

result = operand1 / operand2;

break;

default:

cout << “Unknown operator” ;

}

if(operator==’ +’)

{

result = op1 + op2;

}

else

if(operator==’ -’)

{

result = op1 – op2;

}

else

if (operator==’ *’)

{

__________ __        _ ______

Fantasy.fear@gmail.com

14

result = op1 * op2;

}

else

if (operator==’ /’)

{

result = op1 / op2;

}

else

{

cout << “Unknown operator” ;

}

Q22

Write a recursive function that takes character array and starting subscript as

arguments. In each recursive call, the function should display the string from

subscript to the end of string. The starting subscript in first call should be 0. In

each successive call, the subscript should increse by one and function should

print the array from subscript to the end of string. The function should stop

processing and return when null character encounters.

Suppose the char string passed to the function is,

“SampleString”, then the function will print output as follows,

SampleString

ampleString

mpleString

pleString

so on….

Marks: 10

MIDTERM EXAMINATION

Spring 2009

CS201- Introduction to Programming

__________ __        _ ______

Fantasy.fear@gmail.com

15

Question No: 1 ( Marks: 1 ) – Please choose one

A precise sequence of steps to solve a problem is called

_ Statement

_ Program

_ Utility

_ Routine

Question No: 2 ( Marks: 1 ) – Please choose one

The Compiler of C language is written in

_ Java Language

_ UNIX

_ FORTRON Language

_ C Language

Question No: 3 ( Marks: 1 ) – Please choose one

Initialization of variable at the time of definition is,

_ Must

_ Necessary

_ Good Programming

_ None of the given options

Question No: 4 ( Marks: 1 ) – Please choose one

In if structure the block of statements is executed only,

_ When the condition is false

_ When it contain arithmetic operators

_ When it contain logical operators

_ When the condition is true

Question No: 5 ( Marks: 1 ) – Please choose one

Which of the following function(s) is/are included in stdlib.h header file?

_ double atof(const char *nptr)

_ int atoi(const char *nptr)

_ char *strcpy ( char *s1, const char *s2)

_ 1 and 2 only

Question No: 6 ( Marks: 1 ) – Please choose one

Dealing with structures and functions passing by reference is the most economical

method

_ True

_ False

Question No: 7 ( Marks: 1 ) – Please choose one

__________ __        _ ______

Fantasy.fear@gmail.com

16

Pointer is a variable which store,

_ Data

_ Memory Address

_ Data Type

_ Values

Question No: 8 ( Marks: 1 ) – Please choose one

Preprocessor program perform its function before ______ phase takes place.

_ Editing

_ Linking

_ Compiling

_ Loading

Question No: 9 ( Marks: 1 ) – Please choose one

Which of the following can not be a variable name?

_ area

_ _area

_ 10area

_ area2

Question No: 10 ( Marks: 1 ) – Please choose one

Which looping process is best, when the number of iterations is known?

_ for

_ while

_ do-while

_ all looping processes require that the iterations be known

Question No: 11 ( Marks: 1 ) – Please choose one

Which character is inserted at the end of string to indicate the end of string?

_ new line

_ tab

_ null

_ carriage return

Question No: 12 ( Marks: 1 ) – Please choose one

How many bytes are occupied by declaring following array of characters?

char str[] = “programming”;

_ 10

_ 11

_ 12

_ 13

Question No: 13 ( Marks: 1 ) – Please choose one

__________ __        _ ______

Fantasy.fear@gmail.com

17

Which of the following header file defines the rand() function?

_ iostream.h

_ conio.h

_ stdlib.h

_ stdio.h

Question No: 14 ( Marks: 1 ) – Please choose one

Commenting the code _____________________

_ Makes a program easy to understand for others.

_ Make programs heavy, i.e. more space is needed for executable.

_ Makes it difficult to compile

_ All of the given options.

Question No: 15 ( Marks: 1 ) – Please choose one

What’s wrong with this for loop?

for (int k = 2, k <=12, k++)

_ the increment should always be ++k

_ the variable must always be the letter i when using a for loop

_ there should be a semicolon at the end of the statement

_ the commas should be semicolons

Question No: 16 ( Marks: 1 ) – Please choose one

For which array, the size of the array should be one more than the number of elements in

an array?

_ int

_ double

_ float

_ char

Question No: 17 ( Marks: 1 )

To Which category of the software “Compiler and Interpreter” belongs?

Question No: 18 ( Marks: 1 )

What is the result of the expression x = 2 + 3 * 4 – 4 / 2

Question No: 19 ( 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.

Question No: 20 ( Marks: 3 )

Write down the output of the following code?

int array[7], sum = 0;

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

{

array = i;

sum+= array;

}

cout<< “ Sum = “ <<sum;

__________ __        _ ______

Fantasy.fear@gmail.com

18

ANS

Sum = 21

What will be the output of the following segment of C++ code?

int A[5] = {1 , 2, 3, 4};

int i;

for (i=0; i<5; i++)

{

A = 2*A;

cout << A << ” “;

}

ANS

2 4 6 8 0

Question No: 22 ( Marks: 10 )

Write a C++ program that will determine if a departmental store customer has exceeded

the credit limit on a charge account.

Program should input the following facts in five variables

  1. Account number
  2. Balance at the beginning of month (Beginning balance)
  3. total of all items charged by customer this month (charges)
  4. total of all credits (credits)
  5. allowed credit limit

Calculate the new balance

New balance = Beginning balance + charges – credits

Determine if new balance exceeds the allowed credit limit. For those customers whose

credit limit is exceeded. The program should display the message “Credit Limit

exceeded.”

Q1

In C/C++ the string constant is enclosed

(a) In curly braces

(b) In small braces

(c) In single quotes

(d) In double quotes

Q2

In flow chart, the symbol used for decision making is,

  1. a) Rectangle
  2. b) Circle
  3. c) Arrow
  4. d) Diamond

Q3

The data type before a function name represents its,

__________ __        _ ______

Fantasy.fear@gmail.com

19

(a) Return Type

(b) Function data

(c) Function arguments

(d) Function name

Q4

The operator used to take the address of a variable is,

(a) &&

(b) ++

(c) –

(d) &

Q5

The header file which is used for input and output is

(a) maths.h

(b) string.h

(c) iostream.h

(d) ctype.h

Q6

When we are using command line argument(s), the default argument in C/C++

is/are ____________ .

  1. a) argc
  2. b) argd
  3. c) argv
  4. d) argc and argv

Q7

In C++, Integer calculation occurs in _____ bytes.

  1. a) 1 byte
  2. b) 2 bytes
  3. c) 4 bytes
  4. d) 8 bytes

Q8

In the declaration of two dimensional array,

(a) First index represents row and second represents column

(b) First index represents column and second represents row

(c) Both indexes represent rows

(d) Both indexes represent column

Q9

The address operator (&) can be used with,

  1. a) Statement
  2. b) Expression
  3. c) Variable
  4. d) Constant

Q10

______ translates high level language program into machine language code

(a) Debugger

__________ __        _ ______

Fantasy.fear@gmail.com

20

(b) Editor

(c) Compiler

(d) Linker

Q11

Which of the following data type(s) can operate on modulus operator ‘%’?

  1. a) float, int
  2. b) float, double
  3. c) int
  4. d) char

Q12

What will be the result of the expression z = x % y, if x = 19 and y = 4?

  1. a) 3
  2. b) 4
  3. c) 15
  4. d) 19

Q13

Which character is inserted at the end of string to indicate the end of string?

  1. a) new line
  2. b) tab
  3. c) null
  4. d) carriage return

Q14

What will be the value of i and j in the following code segment?

int i, j ;

int x[5] = {2, 3, 4, 8, 9} ;

int *ptr =&x[2];

i = (*ptr)++ ;

j = *ptr++ ;

  1. a) i = 5, j = 5
  2. b) i = 5, j = 8
  3. c) i = 4, j = 8
  4. d) i = 5, j = 9

Q15

Syntax of union is identical to ______

  1. a) Structure
  2. b) Class
  3. c) Function
  4. d) None of the given options

Q16

Let suppose

Union intorDouble{

Int ival;

Double charvar;

__________ __        _ ______

Fantasy.fear@gmail.com

21

};

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?

  1. a) 2
  2. b) 4
  3. c) 8
  4. d) 12

Q17

To Which category of the software “Compiler and Interpreter” belongs? (1)

System software

Q18

Give the general syntax of definition of structure. (1)

ANS

struct Name

{

// body of the structure

};

Q19

Write the General syntax for the definition of a user defined function. (2)

ANS

return data type Function Name (parameter list with data type)

{

// definition body

}

Q20

What will be the output of following code segment? (3)

int x[5] = {2, 4, 5, 7, 1} ;

int *ptr =&x[2];

cout << (*ptr)++ <<” ” ;

cout << *ptr++ ;

ANS

5 6

Q21

Detect and correct compile time error(s) in the following code.

__________ __        _ ______

Fantasy.fear@gmail.com

22

Hints: The following code pass a value to a function by Reference and print

the value before and after pass. (5)

ANS

int test (int &x)

{

*x = *x + 200;

return x;

}

main()

{

int x = 100;

cout <<” x = ” <<x;

test (*x); // //invalid type of argument uniary

cout<<endl<< ” x = ” <<x;

getch();

return 0;

}

ANS

The following function pass a reference to the function but in the main

body the call mechanism is wrong. The * sign is not used with it .

X = 100 after the function completion x = 300;

Q22

Write a C/C++ program which calculates the aggregate of a student.

The aggregate can be calculated by the formula: (10)

Aggregate = (Matrix_marks*2 + Fsc_marks*4) / 24

If the aggregate of a student is less than 150 then the program should display

message

“You can not be admitted to VU” otherwise display the message

“Congratulation! You admitted in VU “

ANS

#include<iostream.h>

#include<conio.h>

__________ __        _ ______

Fantasy.fear@gmail.com

23

main()

{

int m,f;

float a;

m=254;

f=340;

a = ((m*2) + (f*4))/24;

if(a < 150)

{

cout <<“You can not be admitted to VU”;

}

else

{

cout <<“Congratulation! You admitted in VU “;

}

//Aggregate = (Matrix_marks*2 + Fsc_marks*4) / 24

getche();

}

Q1

What is function of cout ?

(a) To send data to printer

(b) To read data from keyboard

(c) To display message

(d) To display output on the screen

Q2

In Flow chart process is represented by

(a) Rectangle

(b) Arrow symbol

(c) Oval

(d) Circle

Q3

&& is ——————– operator.

(a) An arithmetic

(b) Logical

(c) Relational

(d) Unary

Q4

__________ __        _ ______

Fantasy.fear@gmail.com

24

An over Flow condition occurs when we try to assign a value to a variable which

is,

(a) Less than its maximum size

(b) Greater than its maximum size

(c) With in its range

(d) Equal to its size

Q5

For dereferencing an array element using pointer, we use the operator

(a) &

(b) *

(c) /

(d) +

Q6

In the declaration of two dimensional array,

(a) First index represents row and second represents column

(b) First index represents column and second represents row

(c) Both indexes represent rows

(d) Both indexes represent column

Q7

Which of the following data type(s) can operate on modulus operator ‘%’?

(a) float, int

(b) float, double

(c) int

(d) char

Q8

Which of the following is the correct way to declare a variable x of integer type?

(a) x int ;

(b) integer x ;

(c) int x;

(d) x integer

Q9

Which of the following can not be a variable name?

(a) area

(b) _area

(c) 10area

(d) area2

Q10

Which of the function call is call by value for the following function prototype?

float area (int);

(a) area(&num) ;

(b) area(num) ;

(c) area(int num) ;

(d) area(*num) ;

Q11

Recursive functions are used when there is a repetitive pattern.

(a) True

__________ __        _ ______

Fantasy.fear@gmail.com

25

(b) False

Q12

What will be the range of numbers generated by function rand () % 9?

(a) 0 to 9

(b) 1 to 9

(c) 0 to 8

(d) 1 to 8

Q13

What will be the correct syntax to declare two-dimensional array of float data

type?

(a) float arr{2}{2} ;

(b) float arr[2][2] ;

(c) float arr[2,2] ;

(d) float[2][2] arr ;

Q14

When a function finishes its execution then,

(a) The control return to its Prototype

(b) The control returns to its definition

(c) Control returns to statement following function call

(d) The compiler stop execution of whole program

Q15

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

  1. int arr[2][3] = {4, 8, 9, 2, 1, 6} ;
  2. 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?

(a) (ii) only

(b) (iii) only

(c) (ii) and (iii)

(d) (i) and (iii)

Q16

Editors are used to compile the code.

(a) True

(b) False

Q17

What are global variables? (1)

ANS

Global variables are those that are defined outside of main. It can be

accessed in entire program.

Q18

Is it possible to evaluate the size of structure, if yes then how? (1)

ANS

YES

#include <iostream.h>

__________ __        _ ______

Fantasy.fear@gmail.com

26

#include <stdlib.h>

struct VehicleParts

{

int wheels;

int seats;

VehicleParts()

{

cout << “\n VehicleParts – default constructor”;

}

VehicleParts(int wheels, int seats)

{

this->wheels = wheels; this->seats = seats; cout << “\n VehicleParts –

parameterized constructor”;

}

Q19

Write down the general syntax of switch statement. (22)

ANS

switch ( variable/expression )

{

case constant1 : statementLlist1 ;

case constant2: statementLlist1 ;

default: statementLlist1 ;

}Q20

What will be the output of following code segment? (3)

int x[5] = {2, 4, 5, 7, 1} ;

int *ptr =&x[2];

cout << (*ptr)++ <<” ” ;

cout << *ptr++ ;

ANS

5 6

Q22

What is the difference between compiler and interpreter? (5)

ANS

compiler and interpreter are system software but the difference is that the

complier compile the whole program but the interpreter is complied line by

line. Compiler are efficient in performance.

Q23 (10)

Write a recursive function that takes three arguments (an integer array, starting subscript

‘s’ and ending subscript ‘e’ ).

In first recursive call, the function should display the array from subscript ‘s’ (s = 0) to ‘e’

(e = size of array). In each successive call, the function should print the array from index

s+1 to e. The function should stop processing and return when starting subscript

becomes equal to ending subscript.

For example, if user enters values for array 2, 3, 4, 5, 6 then the recursive function must

__________ __        _ ______

Fantasy.fear@gmail.com

27

display the following output.

2 3 4 5 6

3 4 5 6

4 5 6

5 6

6

ANS

#include<iostream.h>

#include<conio.h>

void recursive(int [],int,int);

void main()

{

int array[5];

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

{

cout<<“\nEnter the “<<i<<” Index number :”;

cin>>array;

}

recursive(array,0,4); //0 is the starting index and 4 is the ending subscript

getche();

}

void recursive(int arr[],int s,int e)

{

if(s!=e+1)

{

for(int i=s;i<=e;i++)

cout<<arr<<“\t”;;

cout<<endl;

s++;

recursive(arr,s,e); //Recursive call

}

}

Q1

What is function of cout ?

(a) To send data to printer

(b) To read data from keyboard

(c) To display message

(d) To display output on the screen

Q2

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

__________ __        _ ______

Fantasy.fear@gmail.com

28

(a) Logical

(b) Arithmetic

(c) Relational

(d) Conational

Q3

In while loop the loop counter must be initialized,

(a) With in the loop

(b) Before entering the loop

(c) At the end of the loop

(d) None of the given options

Q4

Data Size of the file is always _________ the actual size of the file.

(a) Greater than

(b) Equal to

(c) Less than or equal to

(d) None of the above

Q5

The precedence of * is higher than dot operator (.)operator

(a) True

(b) False

Q6

Let ptr1 and ptr2 are pointer variables that points to integer data type then which

one of the following arithmetic is allowed,

(a) ptr1 + ptr2

(b) ptr1 – ptr2

(c) ptr1 * ptr2

(d) ptr1 / ptr2

Q7

Word processor is

(a) Operating system

(b) Application software

(c) Device driver

(d) Utility software

Q8

What will be the range of numbers generated by function rand () % 9?

(a) 0 to 9

(b) 1 to 9

(c) 0 to 8

(d) 1 to 8

Q9

How many bytes will the pointer intPtr of type int move in the following

statement?

intPtr += 3 ;

(a) 3 bytes

(b) 6 bytes

(c) 12 bytes

__________ __        _ ______

Fantasy.fear@gmail.com

29

(d) 24 bytes

Q10

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

pointer ptr?

(a) *ptr = arr ;

(b) ptr = arr ;

(c) *ptr = arr[5] ;

(d) ptr = arr[5] ;

Q11

What will be the output of the following code segment?

char *x = ”programming” ;

cout << *(x+2) << *(x+3) << *(x+5) << *(x+8) ;

(a) prgm

(b) rorm

(c) ogai

(d) ramg

Q12

There is a pointer variable named ptr of type int then address of which type of

variable the ptr will store in it?

(a) variable of type char

(b) variable of type short

(c) variable of type int

(d) variable of type double

Q13

Flow charts explain the working of a program in pictorial format.

(a) True

(b) False

Q14

The object _______________may be used both for file input and file output

(a) fstream,

(b) ifstream,

(c) ofstream,

(d) none of the given options.

Q15

Most efficient method of dealing with structure variables is to define the structure

globally

(a) True

(b) False

Q16

If a variable is passed by value to a function and the function makes some

changes to that variable then it

(a) does not affect the original variable

(b) affects the original variable

(c) causes syntax error

(d) None of the given options

__________ __        _ ______

Fantasy.fear@gmail.com

30

Q17

How does elements of two-dimensional array store into memory? (1)

ANS

Two dimensional arrays are also stored in liner order. As like one dimensional

array.

Q18

What is a structure? (1)

ANS

In structure, we introduce a new data type. “A structure is a collection of

variables under a single name. These variables can different types, and

each has a name that is used to select it from the structure”

Q19 (2)

When a pointer is incremented then how many bytes will it move to change its

address?

ANS

If an integer occupies four bytes in the memory, then the yptr++; will increment its

value by four.

Q20

What happens when we increment a pointer? (3)

ANS

.

Q21

What are the advantages of random access file over sequential access file? (5)

Q22

Write a C/C++ program which calculates the aggregate of a student.

The aggregate can be calculated by the formula: (10)

Aggregate = (Matrix_marks*2 + Fsc_marks*4) / 24

If the aggregate of a student is less than 150 then the program should display

message

“You can not be admitted to VU” otherwise display the message

“Congratulation! You admitted in VU “

__________ __        _ ______

Fantasy.fear@gmail.com

31

ANS

#include<iostream.h>

#include<conio.h>

main()

{

int m,f;

float a;

m=254;

f=340;

a = ((m*2) + (f*4))/24;

if(a < 150)

{

cout <<“You can not be admitted to VU”;

}

else

{

cout <<“Congratulation! You admitted in VU “;

}

//Aggregate = (Matrix_marks*2 + Fsc_marks*4) / 24

getche();

}

Q1

In C/C++ language the header file which is used to perform useful task and

manipulation of character data is

(a) cplext.h

(b) ctype.h

(c) stdio.h

(d) delay.h

Q2

The header file which is used for input and output is

(a) maths.h

(b) string.h

(c) iostream.h

__________ __        _ ______

Fantasy.fear@gmail.com

32

(d) ctype.h

Q3

Suppose int multi[5][10]; when we are using **multi , it means,

(a) Single dereferencing

(b) Single referencing

(c) Double referencing

(d) Double dereferencing

Q4

To access the data members of structure _______ is used.

(a) dot operator (.)

(b) * operator

(c) _ operator

(d) None of given.

Q5

The precedence of * is higher than dot operator (.)operator

(a) True

(b) False

Q6

Which of the following is the starting index of an array in C++?

(a) 0

(b) 1

(c) -1

(d) any number

Q7

When a call to function statement is encountered,

(a) The control transfers to its Prototype

(b) The control transfers to its definition

(c) Control returns to statement following function call

(d) The compiler stops execution of whole program

Q8

A function must always return value.

(a) True

(b) False

Q9

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

(a) 0 – 99

(b) 1 – 99

(c) 0 – 100

(d) 1 – 100

Q10

If a pointer appears on left hand side of an assignment operator then right side of

that assignment operator must be,

(a) Variable name

(b) Address of variable

(c) Variable value

(d) Constant

__________ __        _ ______

Fantasy.fear@gmail.com

33

Q11

Editors are used to compile the code.

(a) True

(b) False

Q12

Which bitwise operator returns false if both bits are 1?

(a) AND

(b) XOR

(c) NOT

(d) OR

Q13

What does !(7) evaluate to in decimal where ! is a NOT operator?

(a) 7

(b) 8

(c) 9

(d) 10

Q14

Structures cannot be passed as Function Parameters

(a) True

(b) False

Q15

When break statement is encountered in a loop body it,

(a) Transfers the control outside from current loop

(b) Transfers the control outside from current program

(c) Enforces the next iteration of loop

(d) Generates compile time error.

Q16

A union is a user-defined data type that contains only _______from its list of members at

a time.

(a) One object

(b) Two objects

(c) Three objects

(d) None of the given options

Q17

Why programming is important? Describe in ONE line. (1)

“A program is a precise sequence of steps to solve a particular problem.”

Q18

Which bitwise operator returns true if both bits are different and returns false if

both bits are same? (1)

ANS

  1. !=
  2. ==

__________ __        _ ______

Fantasy.fear@gmail.com

34

Q19

Why we close a file after use? (2)

ANS

To save our data stored on file. Also this process makes our program fast

and reliable.

Q20

Can you use an assignment operator to assign the value of one C-string to

another? (3)

ANS

Yes we can assign value c-string to another i.e

char a={‘I Love Pakistan’};

char b={‘I Love Pakistan’};

Q21

The statement int Name [2][2]; define a 2×2 array, Write the code which read

data from keyboard for this array. (5)

ANS

for(int i=o; i<2; i++)

{

for (w=0; w<2; w++)

{

cin >>Name[w];

}

cout <<endl;

}

for(int i=o; i<2; i++)

{

for (w=0; w<2; w++)

{

cout<<Name[w]<<” ”;

}

cout <<endl;

}

Q22

Write a program which reads a text file “PlayersInfo.txt” residing in the current

directory. Open the file PlayersInfo.txt in read mode and assign these values to

the struct Player; assume order of the data in the file to be exactly the same as

the order of struct attributes. The struct Player has following attributes

  1. i) Name
  2. ii) Height

__________ __        _ ______

Fantasy.fear@gmail.com

35

iii) Age

  1. iv) Score
  2. v) Game

After reading the file and assigning values to the struct, in the end close the file

PlayersInfo.txt. (10)

Q1

The size of int data type is

(a) 1 bytes

(b) 2 bytes

(c) 3 bytes

(d) 4 bytes

Q2

When the logical operator AND (&&) combine two expressions exp1 and exp2

then the result will be true only,

(a) When both exp1 and exp2 are true

(b) When both exp1 and exp2 are false

(c) When exp1 is true and exp2 is false

(d) When exp1 is false and exp2 is true

Q3

The correct syntax of do-while loop is,

(a) (condition ) while; do { statements; };

(b) { statements; } do-while ();

(c) while(condition); do { statements; };

(d) do { statements; } while (condition);

Q4

_______________ provide communication channels between files and program.

(a) Streams

(b) Language like C++

(c) Function seekg()

(d) None of the above

Q5

All elements of an array must be of,

(a) different data type

(b) float data only

(c) character data only

(d) same data type

Q6

Function seekg() takes ____________ parameter(s).

(a) 0

(b) 1

(c) 2

(d) 3

Q7

Structures help to define program-specific ___________ .

(a) functions

__________ __        _ ______

Fantasy.fear@gmail.com

36

(b) datatypes

(c) Arithmetic operations

(d) None of the given options.

Q8

In the declaration of two dimensional array,

(a) First index represents row and second represents column

(b) First index represents column and second represents row

(c) Both indexes represent rows

(d) Both indexes represent column

Q9

What will be the result of arithmetic expression 6+27/3*3?

(a) 33

(b) 45

(c) 9

(d) 30

Q10

Which of the function call is call by value for the following function prototype?

float area (int);

(a) area(&num) ;

(b) area(num) ;

(c) area(int num) ;

(d) area(*num) ;

Q11

How many bytes are occupied by declaring following array of characters?

char str[] = “programming”;

(a) 10

(b) 11

(c) 12

(d) 13

Q12

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

pointer ptr?

(a) *ptr = arr ;

(b) ptr = arr ;

(c) *ptr = arr[5] ;

(d) ptr = arr[5] ;

Q13

Let ptr1 and ptr2 are pointer variables then which of the following arithmetic

operation is allowed on pointers?

(a) Addition

(b) Subtraction

(c) Multiplication

(d) All of the above

Q14

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

__________ __        _ ______

Fantasy.fear@gmail.com

37

(a) True

(b) False

Q15

When break statement is encountered in a loop body it,

(a) Transfers the control outside from current loop

(b) Transfers the control outside from current program

(c) Enforces the next iteration of loop

(d) Generates compile time error.

Q16

If two programs a and b are trying to open a file xyz.txt at approximately same

time then

(a) Both programs will generate error

(b) One of them will succeed in opening that file and other will fail

(c) Both programs will open the file

(d) One of the program will re-start

Q17

What is meant by array manipulation? (1)

Q18

What will be the value of x after the execution of the following code segment? (1)

int x =10;

int y =30;

int *xptr = &x;

x = *xptr + 10;

Q19 (2)

What is the output of the code given below?

void main()

{

int a=10,b=20;

char x=1,y=0;

if(a,b,x,y)

{

cout << “EXAM”;

}

}

Q20

What is Overflow condition? (3)

ANS

When we try to store larger information in a variable, than a data type can store,

overflow condition occurs.

Q21

Write code which read a string not greater than 20 characters from keyboard

stored it in an array Name and display it on the screen. (5)

ANS

__________ __        _ ______

Fantasy.fear@gmail.com

38

char string[19];

cout<<”Enter a string”;

cin >> string;

Q22 (10)

Write a C++ program which contains a user-define function named

convertHeight which takes height of person in centimeter as an argument. This

function converts the height in centimeter into feet and inches and displays them

on the screen.

Program should prompt the user to enter height in centimeter and pass it to

function convertHeight as an argument which displays height in feet and inches.

Hint:

1 foot = 12 inches

1 inch = 2.5 cm

ANS

#include<iostream.h>

#include<conio.h>

void convertHeight (int);

main()

{

int hight;

cout <<“Enter the High of the Person = “;

cin >> hight;

convertHeight(hight);

getche();

}

void convertHeight (int f)

{

//1 foot = 12 inches

//1 inch = 2.5 cm

int i,c;

i = f*12;

c = i*2.5;

cout <<“high in Feet = “<<f<<endl;

cout <<“high in inches = “<<i<<endl;

cout <<“high in CM = “<<c<<endl;

}

__________ __        _ ______

Fantasy.fear@gmail.com

39

Q1

The data type of size one byte is

(a) char

(b) int

(c) long

(d) double

Q2

If Num is an integer variable then Num*= 4; means,

(a) Multiply Num 4 times

(b) Multiply 4 with Num and display

(c) Multiply 4 with Num and assign the result to Num

(d) Add 4 with Num

Q3

Member function tellg() returns the current location of the _____________

pointer.

(a) tellptr()

(b) write()

(c) seekg()

(d) get()

Q4

If we want to store a string “abc” in an array str then the size of this array must be

at least,

(a) 2

(b) 3

(c) 4

(d) 5

Q5

Pointer is a variable which store,

(a) Values

(b) Data

(c) Memory Address

(d) Data Type

Q6

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

(a) Linux

(b) Windows

(c) Unix

(d) Mac OS

Q7

C++ is a case-sensitive language

(a) True

(b) False

__________ __        _ ______

Fantasy.fear@gmail.com

40

Q8What is the output of the following code?

for (int a = 1; a <= 1; a++) cout << a++; cout << a;

(a) 22

(b) 12

(c) 23

(d) 13

Q9

A continue statement causes execution to skip to

(a)the return 0; statement

(b) the first statement after the loop

(c) the statements following the continue statement

(d) the next iteration of the loop

Q10

If there is more than one statement in the block of a for loop, which of the

following must be placed at the beginning and the ending of the loop block?

(a) parentheses ( )

(b) braces { }

(c) brackets [ ]

(d) arrows < >

Q11

What will be the correct syntax for the following function call?

float square (int &);

(a) square (int num);

(b) square (&num);

(c) square (num);

(d) square (*num);

Q12

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?

(a) m[2][3] = 5 ;

(b) m[3][2] = 5 ;

(c) m[1][2] = 5 ;

(d) m[2][3] = ‘5’;

Q13

How many dimensions does n-dimensional array has?

(a) n dimensions

(b) 2n dimensions

(c) (n+1) dimensions

(d) (n-1) dimensions

Q14

Consider the following code segment. What will be the output of following code?

int addValue (int *a){

int b = (*a) + 2;

return b ;

__________ __        _ ______

Fantasy.fear@gmail.com

41

}

main () {

int x =6 ;

cout << x << “,” ;

cout << addValue(&x) << “,” ;

cout << x ;

}

(a) 6,8,6

(b) 6,6,8

(c) 6,8,8

(d) 6,6,6

Q15

If most significant bit of un-signed number is 1 then it represents a positive

number.

(a) True

(b) False

Q16

When we declare a multidimensional array the compiler store the elements of

multidimensional array in the form of,

(a) Contiguous memory location

(b) Matrix

(c) Columns

(d) Rows

Q17

What is the functionality of the function:char *strncat (char *s1, const char *s2,

size_t n) (1)

ANS

Q18

Write a piece of code that outputs three values of type int, long and float to a

stream. (1)

ANS

main()

{

int a;

long b;

float c;

a = 10;

b= 124568979;

c = 6.57;

cout <<a<<“\t”<<b<<“\t”<<c;

getche();

__________ __        _ ______

Fantasy.fear@gmail.com

42

}

Q19

Which bit of the number is used as a sign bit? (2)

ANS

The most significant bit is used as a sign bit.

Q20

What is difference between single-dimensional and multi-dimensional array?

ANS

Single dimentional array used to stored lists and muli dimestional arrays

used to stored value as tabler formate i.e matrix.

Q21

Write down the C++ program that calculates the Zakat on the amount entered by

the user

Note: Zakat is 2.5% of the given amount (5)

ANS

main()

{

int amount;

float zakat;

cout <<”Enter the Amount = “;

cin >>amount;

zakat = (amount*2.5)/100;

cout <<“Amount = “<<amount<<endl;

cout <<“Amount = “<<zakat<<endl;

getche();

}

Q22

What is meant by scope of identifiers? Differentiate between different scope of identifiers

and explain them with examples? (10)

Now this variable ‘i’ can be used in any statement inside the function func1(). But

consider this variable being used in a different function like:

void func2()

{

__________ __        _ ______

Fantasy.fear@gmail.com

43

int k = i + 4; //Compilation error

}

The variable ‘i’ belongs to func1() and is not visible outside that. In other words, ‘is

local to func1().

Q1

What is function of cout ?

(a) To send data to printer

(b) To read data from keyboard

(c) To display message

(d) To display output on the screen

Q2

For one byte there are _____ combinations of values that can be stored in

computer.

(A) 26

(B) 27

(C) 28

(D) 24

Q3

_______________ provide communication channels between files and program.

(a) Streams

(b) Language like C++

(c) Function seekg()

(d) None of the above

Q4

The data type before a function name represents its,

(a) Return Type

(b) Function data

(c) Function arguments

(d) Function name

Q5

In C/C++ language when an array is passed to a function then by default its

passing mechanism is,

(a) Call by value

(b) Call by Reference

(c) It depends on type of array

(d) It depends on the return type of function.

Q6

Array is a data structure which store

(a) Memory addresses

(b) Variables

(c) Data Type

(d) Data

Q7

If there is more than one statement in the block of a for loop, which of the

following must be placed at the beginning and the ending of the loop block?

__________ __        _ ______

Fantasy.fear@gmail.com

44

(a) parentheses ( )

(b) braces { }

(c) brackets [ ]

(d) arrows < >

Q8

Array is passed by value to a function by default.

(a) True

(b) False

Q9

Which of the following is the correct function call having array named student of

10 elements as a parameter.

(a) addRecord(student[]) ;

(b) addRecord(student) ;

(c) addRecord(student[10]) ;

(d) addRecord(*student) ;

Q10

What will be the correct syntax for initialization of pointer ptr of type int with

variable x?

(a) int ptr = &x ;

(b) int ptr = x ;

(c) int *ptr = &x ;

(d) int ptr* = &x ;

Q11

What will be the correct syntax for initialization of pointer ptr with string

“programming”?

(a) char ptr = ’programming’ ;

(b) char *ptr = “programming” ;

(c) char *ptr = ‘programming’ ;

(d) *ptr = “programming” ;

Q12

The condition in while loop may contain logical expression but not relational

expression.

(a) True

(b) False

Q13

We want to access array in random order which approach is better?

(a) Pointers

(b) Array index

(c) Both pointers and array index are better

(d) None of the given options.

Q14

Single line comments explaining code would be preceded like in the following

example.

(a) /*

(b) //

(c) /

__________ __        _ ______

Fantasy.fear@gmail.com

45

(d) //*

Q15

Function write() takes ________________________ as parameter(s).

(a) String of pointer type

(b) String and no. of bytes to be written

(c) Pointer array of characters and delimiter

(d) String of variable lengths, no. of bytes to be read and flags

Q16

Structure is a collection of ______________ under a single name.

(a) Only Functions

(b) Only Variables

(c) Both Functions and Variables

(d) None of the given options

Q17

What will be the correct syntax to initialize a pointer ‘ptr’ with two-dimensional

array ‘m’? (1)

ANS

int m[2][2];

int * ptr;

ptr = *m;

Q18

Which one of the loop (while or do-while) must be used if it is necessary to

execute a loop at least once? (1)

ANS

do-while loop

Q19

Identify each of the following function as string conversion function or string

manipulation function. (2)

double atof(const char *nptr)

char *strcpy ( char *s1, const char *s2)

int atoi(const char *nptr)

ANS

  1. double atof(const char *nptr) —_Converts the string nPtr to double.
  2. char *strcpy ( char *s1, const char *s2) -_ Copies string s2 into

character array s1. The value of is returned.

  1. int atoi(const char *nptr)–_ Converts the string nPtr to int.

Q20

What is difference between single-dimensional and multi-dimensional array? (3)

ANS

Single dimentional array used to stored lists and muli dimestional arrays

used to stored value as tabler formate i.e matrix.

__________ __        _ ______

Fantasy.fear@gmail.com

46

Q21

What will be the output of following code segment? (5)

int num[10] = {2, 3, 5, 8, 9, 10, 12, 15, 19, 20} ;

int *ptr = num ;

for (int i=0; i<10; i+=2){

cout << *(ptr+i) << “, ”;

}

ANS

2, 5, 9, 12, 19,

Q22

Write a C++ program which contains a user-define function named

convertHeight which takes height of person in centimeter as an argument. This

function converts the height in centimeter into feet and inches and displays them

on the screen. (10)

Program should prompt the user to enter height in centimeter and pass it to

function convertHeight as an argument which displays height in feet and inches.

Hint:

1 foot = 12 inches

1 inch = 2.5 cm

ANS

#include<iostream.h>

#include<conio.h>

void convertHeight (int);

main()

{

int hight;

cout <<“Enter the High of the Person = “;

cin >> hight;

convertHeight(hight);

getche();

}

void convertHeight (int f)

{

//1 foot = 12 inches

//1 inch = 2.5 cm

int i,c;

i = f*12;

c = i*2.5;

__________ __        _ ______

Fantasy.fear@gmail.com

47

cout <<“high in Feet = “<<f<<endl;

cout <<“high in inches = “<<i<<endl;

cout <<“high in CM = “<<c<<endl;

}

Q1

Compiler is a

(a) System software

(b) Application Software

(c) Driver

(d) Editor

Q2

In while loop the loop counter must be initialized,

(a) With in the loop

(b) Before entering the loop

(c) At the end of the loop

(d) None of the given options

Q3

If Num is an integer variable then Num*= 4; means,

(a) Multiply Num 4 times

(b) Multiply 4 with Num and display

(c) Multiply 4 with Num and assign the result to Num

(d) Add 4 with Num

Q4

In C/C++ ,the arguments are passed by _______ to a function by default .

(a) reference

(b) value

(c) data

(d) type

Q5

Disks is divided into ____________ with power of__________.

(a) Chunks, 2n

(b) Blocks, n2

(c) Blocks, 2n

(d) Chunks, n2

Q6

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

(a) Linux

(b) Windows

(c) Unix

(d) Mac OS

Q7

Assignment operator ‘=’ is a

(a) Unary operator

__________ __        _ ______

Fantasy.fear@gmail.com

48

(b) Binary operator

(c) Ternary operator

(d) None of the given options

Q8

Consider the following code segment. What will be the output of the following

program?

int func(int) ;

int num = 10 ;

int main(){

int num ;

num = 5 ;

cout << num ;

cout << func(num) ;

}

int func(int x){

return num ;

}

(a) 5, 5

(b) 10, 5

(c) 5, 10

(d) 10, 10

Q9

Name of an array is a constant pointer.

(a) True

(b) False

Q10

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

pointer ptr?

(a) *ptr = arr ;

(b) ptr = arr ;

(c) *ptr = arr[5] ;

(d) ptr = arr[5] ;

Q11

If there are 2(n+1) elements in an array then what would be the number of

iterations required to search a number using binary search algorithm?

(a) n elements

(b) (n+1) elements

(c) 2(n+1) elements

(d) 2(n+1) elements

Q12

In C/C++, null character is represented as

(a) \n

(b) \0

__________ __        _ ______

Fantasy.fear@gmail.com

49

(c) \t

(d) \r

Q13

How many nested loop would be required to manipulate n-dimensional array?

(a) 2n

(b) n

(c) n +1

(d) n -1

Q14

What will be the correct syntax to access the value of fourth element of an array

using pointer ptr?

(a) ptr[3]

(b) (ptr+3)

(c) *(ptr+3)

(d) Both 1and 3

Q15

Single line comments explaining code would be preceded like in the following

example.

(a) /*

(b) //

(c) /

(d) //*

Q16

If a variable is passed by value to a function and the function makes some

changes to that variable then it

(a) does not affect the original variable

(b) affects the original variable

(c) causes syntax error

(d) None of the given options

Q17

What is meant by *num and &num? [1]

ANS

* num is a pointer and &num is a reference to that pointer.

Q18

Suppose there is a pointer to structure *sPtr. How can we access the data

member ‘name’ with sPtr? [1]

ANS

Structure data members using pointers Using the * operator;

(*sptr).name

Q19

Why we close a file after use? [2]

ANS

To save our data stored on file. Also this process makes our program fast

and reliable

__________ __        _ ______

Fantasy.fear@gmail.com

50

Q20

Define Flow chart. [3]

ANS

Flow Chart

A flow chart is a pictorial representation of a program. There are labeled geometrical

symbols, together with the arrows connecting one symbol with other. A flow chart helps

in correctly designing the program by visually showing the sequence of instructions to be

executed.

Q21

Write down the function definition if we want to pass the arguments to a function by

reference without changing the values stored at that addresses. [5]

Q22

From writing to execution of the program following software are used explain for

what purpose each is used. [10]

Editor

Compiler/Interpreter

Linker

Loader

ANS

Editors is a tool for writing the code of a program. For this purpose we used

Editors in which we write our code. We can use word processor too for this, but

word processors have many other features like bold the text, italic, coloring the

text etc, so when we save a file written in a word processor, lot of other

information including the text is saved on the disk. For programming purposes we

don’t need these things we only need simple text. Text editors are such editors

which save only the text which we type. So for programming we will be using a

text editor

Compiler and Interpreter

Compilers translate the English like language (Code written in C) into a language

(Machine language) which computers can understand. The Compiler read the

whole program and translates it into machine language completely. The

difference between interpreter and compiler is that compiler will stop translating if

it finds an error and there will be no executable code generated whereas

Interpreter will execute all the lines before error and will stop at the line which

contains the error. So Compiler needs syntactically correct program to produce

an executable code. We will be using compiler in our course

As we write the code in English and we know that computers can understand

only 0s and 1s. So we need a translator which translates the code of our program

__________ __        _ ______

Fantasy.fear@gmail.com

51

into machine language. There are two kinds of translators which are known as

Interpreter and Compilers. These translators translate our program which is

written in C-Language into Machine language. Interpreters translates the

program line by line meaning it reads one line of program and translates it, then it

reads second line, translate it and so on. The benefit of it is that we get the errors

as we go along and it is very easy to correct the errors. The drawback of the

interpreter is that the program executes slowly as the interpreter translates the

program line by line. Another drawback is that as interpreters are reading the

program line by line so they cannot get the overall picture of the program hence

cannot optimize the program making it efficient.

.

Linker Most of the time our program is using different routines and functions

that are located in different files, hence it needs the executable code of those

routines/functions. Linker is a tool which performs this job, it checks our program

and includes all those routines or functions which we are using in our program to

make a standalone executable code and this process is called Linking

Loader after a executable program is linked and saved on the disk and it is

ready for execution. We need another process which loads the program into

memory and then instruct the processor to start the execution of the program

from the first instruction (the starting point of every C program is from the main

function). This processor is known as loader. Linker and loaders are the part of

development environment. These are part of system software.

Q1

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

(a) Two

(b) Three

(c) Four

(d) Five

Q2

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

(a) Logical

(b) Arithmetic

(c) Relational

(d) Conational

Q3

In order to get 256 from the number 2568 we divide this number by 10 and take,

(a) Its remainder

(b) The number

(c) Its quotient

(d) Its divisor

Q4

__________ __        _ ______

Fantasy.fear@gmail.com

52

If int x = 10; then the value of x/= 3; will be,

(a) 10

(b) 3

(c) 13

(d) 1

Q5

How many parameter(s) function getline() takes?

(a) 0

(b) 1

(c) 2

(d) 3

Q6

Suppose int multi[5][10]; when we are using **multi , it means,

(a) Single dereferencing

(b) Single referencing

(c) Double referencing

(d) Double dereferencing

Q7

To access the data members of structure _______ is used.

(a) dot operator (.)

(b) * operator

(c) _ operator

(d) None of given.

Q8

There is NO difference between bitwise AND operator (&) and Logical AND

(&&) operator.

(a) True

(b) False

Q9

Which of the following data type(s) can operate on modulus operator ‘%’?

(a) float, int

(b) float, double

(c) int

(d) char

Q10

What’s wrong with this while loop?

while( (i < 10) && (i > 24))

(a) the logical operator && cannot be used in a test condition

(b) the while loop is an exit-condition loop

(c) the while loop is an exit-condition loop

(d) the test condition is always true

Q11

The switch structure is a _____________construct

(a) single-selection

(b) bi-selection

__________ __        _ ______

Fantasy.fear@gmail.com

53

(c) multiple-selection

(d) unconditional

Q12

Keyword ‘array’ must be used to declare an array.

(a) True

(b) False

Q13

What will be the correct syntax to declare two-dimensional array of float data

type?

(a) float arr{2}{2} ;

(b) float arr[2][2] ;

(c) float arr[2,2] ;

(d) float[2][2] arr ;

Q14

When an array element is passed to a function then this array element is passed

to the function,

(a) By reference

(b) By data type

(c) By value

(d) By data

Q15

Which of the following operator is used to access the value of variable pointed to

by a pointer?

(a) * operator

(b) -> operator

(c) && operator

(d) & operator

Q16

Paying attention to detail in designing a program is _________

(a) Time consuming

(b) Redundant

(c) Necessary

(d) Somewhat Good

Q17

How does elements of two-dimensional array store into memory? (1)

Two dimensional arrays are also stored in liner order. As like one dimensional

array.

Q18

Which strategy is used by binary search algorithm to search a number? (1)

ANS

‘divide and conquer’ strategy is applied.

Q19

Write down the general syntax of switch statement. (2)

ANS

__________ __        _ ______

Fantasy.fear@gmail.com

54

switch ( variable/expression )

{

case constant1 : statementLlist1 ;

case constant2: statementLlist1 ;

default: statementLlist1 ;

}

Q20

What is a Linker? (3)

Linker Most of the time our program is using different routines and functions

that are located in different files, hence it needs the executable code of those

routines/functions. Linker is a tool which performs this job, it checks our program

and includes all those routines or functions which we are using in our program to

make a standalone executable code and this process is called Linking

Q21

What are similarities and differences between Structures and Unions? (5)

Structure

In structures, the data members are public by default. It means that these are

visible to all and anyone can change them. Is there any disadvantage of this?

Think about the date.

syntax

struct student

{

char name[60];

char address[100];

float GPA;

};

Unions We have another construct named union. The concept of union in C/C++

is: if we have something in the memory, is there only one way to access that

memory location or there are other ways to access it. We have been using int

and char interchangeably in our programs. We have already developed a

program that prints the ACSII codes. In this program, we have stored a char

inside an integer. Is it possible to have a memory location and use it as int or

char interchangeably? For such purposes, the construct union is used. The

syntax of union is:

union intOrChar

{

int i;

char c;

};

Q22

Differentiate between C and c++. (10)

  1. C was the C++ predecessor. As it’s name implies, alot of C remains in

__________ __        _ ______

Fantasy.fear@gmail.com

55

C++. Although not actually being more powerful than C.

  1. C++ allows the programmer to more easily manage and operate with Objects,

using an OOP (Object Oriented Programming) concept

  1. C++ allows the programmer to create classes, which are somewhat similar to C

structures. However, to a class can be assigned methods, functions associated to

it, of various prototypes, which can access and operate within the class, somewhat

like C functions often operate on a supplied handler pointer.

  1. Although it is possible to implement anything which C++ could implement in C,

C++ aids to standardize a way in which objects are created and managed, whereas

the C programmer who implements the same system has a lot of liberty on how to

actually implement the internals, and style among programmers will vary a lot on

the design choices made

  1. In C, some will prefer the handler-type, where a main function initializes a

handler, and that handler can be supplied to other functions of the library as an

object to operate on/through. Others will even want to have that handler link all

the related function pointers within it which then must be called using a

convention closer to C++.

  1. C++ applications are generally slower at runtime, and are much slower to compile

than C programs. The low-level infrastructure for C++ binary execution is also

larger. For these reasons C is always commonly used even if C++ has alot of

popularity, and will probably continue to be used in projects where size and speed

are primary concerns, and portable code still required (assembly would be

unsuitable then).

Q1

The remainder (%) operator is

(a) A logical operator

(b) An arithmetic operator

(c) A relational operator

(d) A division operator

Q2

If int sum = 10; then the value of the statement sum = sum + 3 ; is ,

(a) 7

(b) Illegal statement

(c) Garbage value

(d) 13

Q3

Which of the following function(s) is/are included in ctype.h header file?

(a) isdigit(int c)

(b) isxdigit(int c )

(c) tolower(int c)

(d) All of the above

Q4

In C/C++ which of the following header file is used for string manipulation?

(a) stdlib.h

(b) string.h

__________ __        _ ______

Fantasy.fear@gmail.com

56

(c) strings.h

(d) stype.h

Q5

_______________ provide communication channels between files and program.

(a) Streams

(b) Language like C++

(c) Function seekg()

(d) None of the above

Q6

______ translates high level language program into machine language code

(a) Debugger

(b) Editor

(c) Compiler

(d) Linker

Q7

Which of the following data type(s) can operate on modulus operator ‘%’?

(a) float, int

(b) float, double

(c) int

(d) char

Q8

C++ is a case-sensitive language

(a) True

(b) False

Q9

To include code from the library in the program, such as iostream, a directive

would be called up using this command.

(a) #include “iostream.h”

(b) include <iostream.h>

(c) include <iostream.h>

(d) #include <iostream.h>

Q10

What will be the range of numbers generated by function rand () % 9?

(a) 0 to 9

(b) 1 to 9

(c) 0 to 8

(d) 1 to 8

Q11

An array stores the numbers into consecutive memory locations.

(a) True

(b) False

Q12

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

const int *ptr.

(a) ptr is a constant pointer

(b) ptr is constant integer pointer

__________ __        _ ______

Fantasy.fear@gmail.com

57

(c) ptr is a constant pointer to int

(d) ptr is a constant pointer to int

Q13

Which of the following header file defines the rand() function?

(a) iostream.h

(b) conio.h

(c) stdlib.h

(d) stdio.h

Q14

Consider the following code segment. What will be the output of following code?

int addValue (int *a){

int b = (*a) + 2;

return b ;

}

main () {

int x =6 ;

cout << x << “,” ;

cout << addValue(&x) << “,” ;

cout << x ;

}

(a) 6,8,6

(b) 6,6,8

(c) 6,8,8

(d) 6,6,6

Q15

Identifier is a name that can be given to variables, labels and functions.

(a) True

(b) False

Q16

For which array, the size of the array should be one more than the number of

elements in an array?

(a) Int

(b) Char

(c) Double

(d) float

Q17

Give a precise definition of function . (1)

ANS

Functions In C/C++, functions are a way of modularizing the code. A bigger problem

is broken down into smaller and more manageable parts. There is no rule of thumb for the

length of each part but normally one function’s length is not more than one screen.

Q18

What will be the size of array if we initialize an array with declaration: int arr[] =

{0, 0, 0, 0};? (1)

__________ __        _ ______

Fantasy.fear@gmail.com

58

ANS

int arr[3];

Q19

What is the difference between switch statement and if statement. (2)

ANS

In switch statement only one variable can be tested on various condition but using if we

can tested multi variables in single statement

Q20

Evaluate the following arithmetic expressions. (3)

  1. a) X = 2 + 6 * 4– 4 * 20 / 5 +3 * 2
  2. b) Y = (6 * 7) – (2 + 3) * (3 – 1) + 5 * (3 +1)

ANS

  1. a) X = 2 + (6 * 4)–( (4 * 20) / 5) + (3 * 2)
  2. b) Y =( (6 * 7) – (2 + 3) * (3 – 1) +( 5 * (3 +1)))
  3. a) 16
  4. b) 52

Q21

What is the difference between = in C as compared to = used in algebra. (5)

ANS

In C = sign is used to assigned the value in algebra = sign is show that the both

side are equal.

In C there must be a variable on the = sign and the right side of the = sign must

be a arithmetic expression, variable or a value.

In algebra both side of the equation may or may not be expression.

Q22

Write a program which consists of three variables Area, Per, Base,

this program should find the area of triangle using the formula, (10)

Area = (Base * Per)/2

#include<iostream.h>

#include<conio.h>

main()

{

float Area, Per, Base;

// Area = (Base * Per)/2

Per = 5.0;

Base = 2.5;

Area = (Base * Per)/2;

cout <<“Area = “<<Area;

getche();

__________ __        _ ______

Fantasy.fear@gmail.com

59

}

Q1

In C/C++ language the header file which is used to perform useful task and

manipulation of character data is

(e) cplext.h

(f) ctype.h

(g) stdio.h

(h) delay.h

Q2

The header file which is used for input and output is

(e) maths.h

(f) string.h

(g) iostream.h

(h) ctype.h

Q3

Suppose int multi[5][10]; when we are using **multi , it means,

(e) Single dereferencing

(f) Single referencing

(g) Double referencing

(h) Double dereferencing

Q4

To access the data members of structure _______ is used.

(e) dot operator (.)

(f) * operator

(g) _ operator

(h) None of given.

Q5

The precedence of * is higher than dot operator (.)operator

(c) True

(d) False

Q6

Which of the following is the starting index of an array in C++?

(e) 0

(f) 1

(g) -1

(h) any number

Q7

When a call to function statement is encountered,

(e) The control transfers to its Prototype

(f) The control transfers to its definition

(g) Control returns to statement following function call

(h) The compiler stops execution of whole program

Q8

A function must always return value.

__________ __        _ ______

Fantasy.fear@gmail.com

60

(c) True

(d) False

Q9

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

(e) 0 – 99

(f) 1 – 99

(g) 0 – 100

(h) 1 – 100

Q10

If a pointer appears on left hand side of an assignment operator then right side of

that assignment operator must be,

(e) Variable name

(f) Address of variable

(g) Variable value

(h) Constant

Q11

Editors are used to compile the code.

(c) True

(d) False

Q12

Which bitwise operator returns false if both bits are 1?

(e) AND

(f) XOR

(g) NOT

(h) OR

Q13

What does !(7) evaluate to in decimal where ! is a NOT operator?

(e) 7

(f) 8

(g) 9

(h) 10

Q14

Structures cannot be passed as Function Parameters

(c) True

(d) False

Q15

When break statement is encountered in a loop body it,

(e) Transfers the control outside from current loop

(f) Transfers the control outside from current program

(g) Enforces the next iteration of loop

(h) Generates compile time error.

Q16

A union is a user-defined data type that contains only _______from its list of members at

a time.

(e) One object

__________ __        _ ______

Fantasy.fear@gmail.com

61

(f) Two objects

(g) Three objects

(h) None of the given options

Q17

Why programming is important? Describe in ONE line. (1)

Q18

Which bitwise operator returns true if both bits are different and returns false if

both bits are same? (1)

Q19

Why we close a file after use? (2)

Q20

Can you use an assignment operator to assign the value of one C-string to

another? (3)

Q21

The statement int Name [2][2]; define a 2×2 array, Write the code which read

data from keyboard for this array. (5)

Q22

Write a program which reads a text file “PlayersInfo.txt” residing in the current

directory. Open the file PlayersInfo.txt in read mode and assign these values to

the struct Player; assume order of the data in the file to be exactly the same as

the order of struct attributes. The struct Player has following attributes

  1. i) Name
  2. ii) Height

iii) Age

  1. iv) Score
  2. v) Game

After reading the file and assigning values to the struct, in the end close the file

PlayersInfo.txt. (10)

Q23

Q2

Write a program to convert upper case letter to lower case letter. (5)

Write a program to convert lower case letter to upper case letter. ( 5)

__________ __        _ ______

Fantasy.fear@gmail.com

62

Q2

Write a program to convert upper case letter to lower case letter. (5)

Ans:

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <stdlib.h>

int main()

{

char inputString[100];

char lowerCase[100];

cout<<“Please enter a string (maximum 100 characters): “;

gets(inputString);

int i=0;

for(i=0; i<strlen(inputString); i++)

{

lowerCase = tolower(inputString);

}

lowerCase=’\0′;

puts(lowerCase);

system(“PAUSE”);

return 0;

}

Write a program to convert lower case letter to upper case letter. ( 5)

Ans:

__________ __        _ ______

Fantasy.fear@gmail.com

63

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

#include <stdlib.h>

int main()

{

char inputString[100];

char upperCase[100];

cout<<“Please enter a string (maximum 100 characters): “;

gets(inputString);

int i=0;

for(i=0; i<strlen(inputString); i++)

{

upperCase = toupper(inputString);

}

upperCase=’\0′;

puts(upperCase);

system(“PAUSE”);

return 0;

}

 

cs201solvedmidtermpapers1-1

cs201solvedmidtermpapers1

cs201solvedmidtermpapers2

cs2012011

Share with Friends