Docsity

CS201 - Introduction to Programming: Finalterm Examination (Spring 2009), Exams of Computer Programming

Toggle sidebar
bg1
FINALTERM EXAMINATION
Spring 2009
CS201- Introduction to Programming (Session - 1)
Question No: 1 ( Marks: 1 ) - Please choose one
To access the data members of structure _______ is used.
► dot operator (.)
► * operator
► à operator
► None of given.
Question No: 2 ( Marks: 1 ) - Please choose one
What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?
► 1
► 2
► 3
► 4
Question No: 3 ( Marks: 1 ) - Please choose one
If constructor contains a return statement in its body then compiler will give
__________
► No error
► Syntax error
► Logical error
► Run time error
Question No: 4 ( Marks: 1 ) - Please choose one
We can use New keyword inside of Class Constructor.
►True
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS201 - Introduction to Programming: Finalterm Examination (Spring 2009) and more Exams Computer Programming in PDF only on Docsity!

FINALTERM EXAMINATION

Spring 2009

CS201- Introduction to Programming (Session - 1)

[email protected]

Question No: 1 ( Marks: 1 ) - Please choose one To access the data members of structure _______ is used. ► dot operator (.)

► * operator

► à operator

► None of given.

Question No: 2 ( Marks: 1 ) - Please choose one What does 5 ^ 6 , evaluate to in decimal where ‘^’ is Exclusive OR operator?

► 1 ► 2 ► 3 ► 4

Question No: 3 ( Marks: 1 ) - Please choose one If constructor contains a return statement in its body then compiler will give


► No error

► Syntax error

► Logical error

► Run time error

Question No: 4 ( Marks: 1 ) - Please choose one We can use New keyword inside of Class Constructor.

►True

 ►False

Question No: 5 ( Marks: 1 ) - Please choose one When an operator function is define as member function for a Unary operator then the number of argument it take is/are,

►Zero

► One

►Two

►N arguments

The declarator of Plus (+) member operator function is

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

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

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

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

Question No: 7 ( Marks: 1 ) - Please choose one Friend function of a class is ______________.

►Member function

► Non-member function

►Private function

►Public function

Question No: 8 ( Marks: 1 ) - Please choose one We can also create an array of user define data type.

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

► int arr[2][3] = {0,0} ;

►int arr[2][3] = {{0},{0}} ;

►int arr[2][3] = {0},{0} ;

►int arr[2][3] = {0} ;

Question No: 15 ( Marks: 1 ) - Please choose one When an operator function is define as member function then operand on the left side of operator must be an object.  ►True ►False Question No: 16 ( Marks: 1 ) - Please choose one break statement can be used outside a loop or switch statement.

 ►True ►False

Question No: 17 ( Marks: 1 ) - Please choose one The keyword_______ is used to return some value from a function.

► return ►break ►continue ►goto

Question No: 18 ( Marks: 1 ) - Please choose one Every data member or function inside the structure is ___ by default whereas everything declared inside a class is ____ by default.

►private, public  ►public, private ►private, protected

►public, protected

Question No: 19 ( Marks: 1 ) - Please choose one Which of the following is true for the C++ statement given below? int &ref = val ;

►It creates a synonym for variable ‘val’ ►It creates an alias for variable ‘val’ ►It’s a new name for variable ‘val’  ►All of the given options

Question No: 20 ( Marks: 1 ) - Please choose one If overloaded plus operator is implemented as non-member function then which of the following statement will be true for the statement given below? obj3 = obj1 + obj2 ;

►obj2 will be passed as an argument to + operator whereas obj2 will drive the + operator ►obj1 will drive the + operator whereas obj2 will be passed as an argument to + operator ► Both objects (obj1, obj2) will be passed as arguments to the + operator ►Any of the objects (obj1, obj2) can drive the + operator

Question No: 21 ( Marks: 1 ) - Please choose one Unary operator implemented as member function takes ____ arguments whereas non- member function takes _____ arguments.

►One, zero  ►Zero, one ►One, two

Question No: 26 ( Marks: 1 ) - Please choose one Consider the following code segment.

class M { public: M &operator+(const M &); ... };

p + q //code of line implies that p.operator+(q) ... Let assume if p and q are class objects then function is implemented as _______

► Member function

►Non-member function

►Friend function

►None of the given options

Question No: 27 ( Marks: 1 ) - Please choose one Assignment operator is -------------------------associative.

►right

►left

 ►binary

►unary

Question No: 28 ( Marks: 1 ) - Please choose one Static variable which is defined in a function is initialized __________. ► Only once during its life time ► Every time the function call ► Compile time of the program ► None of the above

Question No: 29 ( Marks: 1 ) - Please choose one Wecan not define a variable of user-defined data type in the class.

►True  ►False

Question No: 30 ( Marks: 1 ) - Please choose one A constructor that will create a new object with a full copy of the other object, is copy is known as ___________ ► deep copy ►shallow copy ►constructor copy ►none of the options

Question No: 31 ( Marks: 1 ) What is the use of reference data type?

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 No: 32 ( Marks: 1 ) What are the main types of operators in terms of number of arguments they take?

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 (see below example). When the function defined for the binary operator overloading is a friend function, then it uses two arguments.

Question No: 33 ( Marks: 2 ) What is the this pointer? Give an example of its use 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 No: 34 ( Marks: 2 ) What are manipulators? Give one example.

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 No: 37 ( Marks: 3 ) 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?

Question No: 38 ( Marks: 5 ) Write down the disadvantages of the templates.

The disadvantages of templates are:

  • 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: 39 ( Marks: 5 ) The following code segment has errors. Locate as many as you can and explain briefly.

class Circle // no need to enter colon here , so I removed it { private : //colon missing double centerX; double centerY; double radius; public: //colon missing void setCenter(double, double); void setRadius(int); };//semi colon missing

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

Please find the Answer in following Box

#include <stdio.h> #include #include 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(); }; void Date: :setDay(int d) { if{d<1 | | d>31) cout<<"Invalid month Renter it"; cin>>d; } day=d; }

void Date: :setMonth (int m)

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 No: 41 ( 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

Find solution in following box:

#include <stdio.h> #include #include

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