CBSE 12th class Paper Solved – 2015-16 session
Sample Question Paper – Set II
Computer Science (083)
Class- XII (2015-16)
Time: 3hrs M.M: 70
Instructions:
i. All Questions are Compulsory.
ii. Programming Language: Section A : C++
iii. Programming Language: Section B: Python
iv. Answer either Section A or B, and Section C is compulsory
Section : A (C++)
Q1 a. Define Macro with suitable example. 2
Ans: Macros are preprocessor directive created using # define that serve as symbolic constants. They are a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. They are created to simplify and reduce the amount of repetitive coding.
For instance,
CPP
#define sum (a, b) a + b |
Defines the macro sum, taking two arguments a and b. This macro may be called like any function. Therefore, after preprocessing:
CPP
z = sum(x, y); is replaced with Z = x + y; |
b. Which C++ header file (s) will be included to run /execute the following C++ code? 1
CPP
void main() { int Last = 26.5698742658; cout << setw(5) << setprecision(9) << Last; } |
Ans: The two header files that need to be included are:
iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout etc.
iomanip: iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.
c. Rewrite the following program after removing any syntactical errors. Underline each correction made. 2
CPP
#include<iostream.h> void main() int A[10]; A = [ 3, 2, 5, 4, 7, 9, 10 ]; for (p = 0; p <= 6; p++) { if (A[p] % 2 = 0) int S = S + A[p]; } cout << S; } |
Ans: In the above code, curly bracket after main() is missing, the elements of the array are wrongly declared, variable p is not declared and S can’t be declared inside the block. So, the following corrections are made:
#include <iostream.h> void main() { int A[10] = { 3, 2, 5, 4, 7, 9, 10 }; int S = 0, p; for (p = 0; p <= 6; p++) { if (A[p] % 2 == 0) S = S + A[p]; } cout << S; }
d. Find the output of the following C++ program: 2
CPP
#include <iostream.h> void repch( char s[]) { for ( int i = 0; s[i] != '\0' ; i++) { if (((i % 2) != 0) && (s[i] != s[i + 1])) { s[i] = '@' ; } else if (s[i] == s[i + 1]) { s[i + 1] = '!' ; i++; } } } void main() { char str[] = "SUCCESS"; cout << "Original String" << str repch(str); cout << "Changed String" << str; } |
Ans: Output: Replace ‘@’ in place of U, ‘!’ in place of second C and ‘!’ in place of third S
Original String SUCCESS Changed String S@C!ES!
e. Find the output of the following : 3
CPP
#include <iostream.h> void switchover( int A[], int N, int split) { for ( int K = 0; K < N; K++) if (K < split) A[K] += K; else A[K] *= K; } void display( int A[], int N) { for ( int K = 0; K < N; K++) (K % 2 == 0) ? cout << A[K] << "%" : cout << A[K] << endl; } void main() { int H[] = { 30, 40, 50, 20, 10, 5 }; switchover(H, 6, 3); display(H, 6); } |
Ans: In the above program, when switchover () function is called by passing the address of array, size of array and split = 3. In this function for K = 0, 1, 2, if condition is executed and K is added to the array elements i.e. 30+0 = 30, 40+1 = 41, 50+2 = 52 and after that else condition is followed and array elements are multiplied, 20*3 = 60, 10*4 = 40, 5*5 = 25.
Display () prints the value of the modified array and the output is:
30%41 52%60 40%25
f. Observe the following C++ code and find out, which out of the given options i) to iv) are the expected correct output. Also assign the maximum and minimum value that can be assigned to the variable ‘Go’. 2
CPP
void main() { int X[4] = { 100, 75, 10, 125 }; int Go = random(2) + 2; for ( int i = Go; i < 4; i++) cout << X[i] << "$$"; } |
i. 100$$75 ii. 75$$10$$125$$ iii. 75$$10$$ iv.10$$125$$
Ans: Option (iv) can be the only correct answer if Go=2. In this case 10$$125$$ will be printed.
Minimum value of Go = 2 (when random(2) = 0)
Maximum value of Go = 3 (when random(2) = 1)
Q2 a. Differentiate between data abstraction and data hiding. 2
Ans: Data hiding can be defined as the mechanism of hiding the data of a class from the outside world. This is done to protect the data from any accidental or intentional access. Data hiding is achieved by making the members of the class private.
Data abstraction refers to, providing only essential information to the outside world and hiding their background details. Members defined with a public label are accessible to all parts of the program. The data abstraction view of a type is defined by its public members.
b. Answer the questions (i) and (ii) after going through the following class : 2
CPP
class Exam { int Rollno; char Cname[25]; float Marks; public : Exam() // Function 1 { Rollno = 0; Cname =""; Marks = 0.0; } Exam( int Rno, char candname) // Function 2 { Rollno = Rno; strcpy (Cname, candname); } ~Exam() // Function 3 { cout << "Result will be intimated shortly" << endl; } void Display() // Function 4 { cout << "Roll no :"<< Rollno; cout <<"Name :" << Cname; cout <<" Marks :"<< Marks; } }; |
(i) Which OOP concept does Function 1 and Function 2 implement. Explain?
Ans: Function 1 and Function 2 are Constructor, which implement the concept Overloading and Polymorphism, as multiple definitions for Constructors are given in the same scope. Function 1 is a Default constructor and function 2 is a Parameterized constructor.
(ii) What is Function 3 called? When will it be invoked?
Ans: Function 3 is a Destructor which is invoked when the object goes out of scope.
c. Define a class Candidate in C++ with the following specification : 4
Private Members :
- A data members Rno(Registration Number) type long
- A data member Cname of type string
- A data members Agg_marks (Aggregate Marks) of type float
- A data members Grade of type char
- A member function setGrade () to find the grade as per the aggregate marks obtained by the student. Equivalent aggregate marks range and the respective grade as shown below:
Aggregate Marks | Grade |
---|---|
>=80 | A |
Less than 80 and >=65 | B |
Less than 65 and >=50 | C |
Less than 50 | D |
Public members:
- A constructor to assign default values to data members: Rno=0, Cname=”N.A”, Agg_marks=0.0
- A function Getdata () to allow users to enter values for Rno. Cname, Agg_marks and call function setGrade () to find the grade.
- A function dispResult( ) to allow user to view the content of all the data members.
Ans: :
CPP
class Candidate { long Rno; char Cname[20]; float Agg_marks; char Grade; void setGrade() { if (Agg_marks >= 80) Grade = ‘A’; else if (Agg_marks < 80 && Agg_marks >= 65) Grade = ‘B’; else if (Agg_marks < 65 && Agg_marks >= 50) Grade =’C’; else Grade =’D’; } public : Candidate() { Rno = 0; Strcpy(Cname, "N.A."); Agg_marks = 0.0; } void Getdata() { cout << "Registration No"; cin >> Rno; cout << "Name"; cin >> Cname; cout << Aggregate Marks "; cin >> Agg_marks; setGrade(); } void dispResult() { cout << "Registration No" << Rno; cout << "Name" << Cname; cout << Aggregate Marks "<< Agg_marks; } |
d. Give the following class definition answer the question that is follows: 4
CPP
class University { char name[20]; protected : char vc[20]; public : void estd(); void inputdata(); void outputdata(); } class College : protected University { int regno; protected char principal() public : int no_of_students; void readdata(); void dispdata ( ); }; class Department : public College { char name[20]; public : void fetchdata( int ); void displaydata(); }; |
i) Name the base class and derived class of college. 1
Ans:
Base class: University
Derived class: Department
ii) Name the data member(s) that can be accessed from function displaydata().
Ans: char name[20], char HOD[20], char principal(), int no_of_students, char vc[20]
iii) What type of inheritance is depicted in the above class definition?
Ans: Multilevel Inheritance
iv) What will be the size of an object (in bytes) of class Department?
Ans: 85 bytes
Q3 a. An integer array A [30][40] is stored along the column in the memory. If the element A[20][25] is stored at 50000, find out the location of A[25][30]. 3
Ans:
A[i][j] = B + W[No.of rows x (I – Lr) + (J – Lc)]
A[20][25] = B + 2[30 x (20 – 0) + (25 – 0)]
50000 = B + 2[30 x (20 – 0) + (25 – 0)]
B = 48750
A[7][10] = 48750 + 2[30 x (7 – 0) + (10 – 0)]
A[7][10] = 49190
b. Write the definition of functions for the linked implemented queue containing passenger information as follows: 4
CPP
struct NODE { int Ticketno; char PName[20]; NODE* NEXT; }; class Queueofbus { NODE *Rear, *Front; public : Queueofbus() { Rear = NULL; Front = NULL; }; void Insert(); void Delete(); ~Queueofbus() { cout << "Object destroyed"; } }; |
Ans:
CPP
void Queueofbus::Insert() { NODE* p = new NODE; cout << "Enter Ticket no"; cin >> p->Ticketno; cout << "Enter Name"; cin >> p->PName; p->NEXT = NULL; if (rear == NULL) { Rear = p; Front = Rear; } else { Rear->NEXT = p; Rear = Rear->NEXT; } } |
c. Write a function to sort any array of n elements using insertion sort. Array should be passed as argument to the function. 3
Ans:
CPP
void insertsort( int a[], int n) { int p, ptr; // Assuming a[0] = int_min i.e. smallest integer for (p = 1; p <= n; p++) { temp = a[p]; ptr = p - 1; while (temp <= a[ptr]) { a[ptr + 1] = a[ptr]; // Move Element Forward ptr--; } a[ptr + 1] = temp; // Insert Element in Proper Place } |
d. Write a function NewMAT(int A[][], int r, int c ) in C++, which accepts a 2d array of integer and its size as parameters divide all those array elements by 6 which are not in the range 60 to 600(both values inclusive) in the 2d Array . 2
Ans:
CPP
void NewMAT( int A[][], int r, int c) { for ( int i = 0; i < r; i++) for (j = 0; j < c; j++) if ((A[i][j] >= 60) && (A[i][j] <= 600)) A[i][j] /= 6; or A[i][j] = A[i][j] / 6; } |
e. Evaluate the following postfix expression using stack and show the contents after execution of each Operations : 470, 5, 4, ^, 25, /, 6, * 2
Ans:
Q4 a. Consider a file F containing objects E of class Emp. 1
i) Write statement to position the file pointer to the end of the file
Ans: F.seekg(0, ios::end);
ii) Write statement to return the number of bytes from the beginning of the file to the current position of the file pointer.
Ans: F.tellg();
b. Write a function RevText() to read a text file ” Input.txt ” and Print only word starting with ‘I’ in reverse order . 2
Example: If value in text file is: INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY
Ans:
CPP
void RevText() { ifstream in("Input.txt"); char word[25]; while (in) { in >> word; if (word[0] ==’I’) cout << rrev(word); else cout << word; |
c. Write a function in C++ to search and display details, whose destination is “Chandigarh” from binary file “Flight.Data”. Assuming the binary file is containing the objects of the following class: 3
CPP
class FLIGHT { int Fno; // Flight Number char From[20]; // Flight Starting Point char To[20]; // Flight Destination public : char * GetFrom(); { return from; } char * GetTo(); { return To; } void input() { cin >> Fno >> ; gets (From); get(To); } void show() { cout Fno << ": " From ":" To endl; } }; |
Ans:
CPP
void Dispdetails() { ifstream fin("Flight.Dat"); Flight F; while (fin) { fin.read(( char *)&F, sizeof (F)) if ( strcmp (F.GetTo(), "Chandigarh")) F.show(); } } |
Section : B (Python)
Q1 a. List one similarity and one difference between List and Dictionary datatype. 2
Ans:
Similarity: Both List and Dictionary are mutable datatypes.
Dissimilarity: List is a sequential data type i.e. they are indexed.
Dictionary is a mapping datatype. It consists of key: value pair.
Eg: L =[1, 2, 3, 4, 5] is a list
D= {1:”Ajay”, 2:”Prashant, 4:”Himani”} is a dictionary where 1, 2, 4 are keys and “Ajay”, Prashant, “Himani” are their corresponding values.
b. Observe the following Python functions and write the name(s) of the module(s) to which they belong: 1
i) uniform() ii) findall()
Ans: i) random ii) re
c. Rewrite the following Python program after removing all the syntactical errors (if any), underlining each correction: 2
Python
def checkval : x = raw_input ("Enter a number") if x % 2 = 0 : print x, " is even" else if x < 0 : print x, "should be positive" else ; print x, " is odd" |
Ans:
def checkval(): x = raw_input("Enter a number") if x % 2 == 0: print x, "is even" elif x < 0 : print x, "should be positive" else: print x, "is odd"
d. Find the output of the following Python program: 3
Python3
def makenew(mystr): newstr = " " count = 0 for i in mystr: if (count % 2 ! = 0 ): newstr = newstr + str (count) else : if (i.islower()): newstr = newstr + i.upper() else : newstr = newstr + i count + = 1 newstr = newstr + mystr[: 1 ] print ("The new string is :", newstr) makenew("sTUdeNT") |
Ans: The new string is: S1U3E5Ts
e. Find the output of the following program 2
CPP
def calcresult(): i = 9 while i > 1: if (i % 2 == 0): x = i % 2 i = i - 1 else : i = i - 2 x = i print x * *2 |
Ans:
Output:
49 25 9 1
f. Observe the following Python code and find out, which out of the given options i) to iv) are the expected correct output(s). Also assign the maximum and minimum value that can be assigned to the variable ‘Go’. 2
Python3
import random X = [ 100 , 75 , 10 , 125 ] Go = random.randint( 0 , 3 ) for i in range (Go) : print X[i], "$$", |
i. 100$$75$$10$$ ii. 75$$10$$125$$ iii. 75$$10$$ iv.10$$125$$100
Ans:
i. 100$$75$$10$$
Minimum Value that can be assigned to Go is 0
Maximum Value that can be assigned to Go is 3
Q2 a. Discuss the strategies employed by python for memory allocation? 2
Ans: Python uses two strategies for memory allocation- Reference counting and Automatic garbage collection:
Reference Counting: works by counting the number of times an object is referenced by other objects in the system. When an object’s reference count reaches zero, Python collects it automatically.
Automatic Garbage Collection: Python schedules garbage collection based upon a threshold of object allocations and object de-allocations. When the number of allocations minus the number of deallocations are greater than the threshold number, the garbage collector is run and the unused block of memory is reclaimed.
b. Answer the questions (i) and (ii) after going through the following class definition: 2
class Toy : tid =0; tcat = " " def __init__(self):// Function1 10 ..................................... // Blank 2
i. Explain relevance of Function 1.
Ans: __init__ function is used to initialize the members of a class. It is automatically invoked when the object of the class is created.
ii (a). Fill in the blanks with a statement to create object of the class TOY.
Ans: T=Toy()
(b). Write statement to check whether tprice is an attribute of class TOY.
Ans: hasattr(T, tprice)
c. Define a class Train in PYTHON with following description: 4
Data Members
src of type string
Tnm of type string
dest of type string
charges of float
• A member function Getdata to assign the following values for Charges
Dest | Charges |
---|---|
Mumbai | 1000 |
Chennai | 2000 |
Kolkata | 2500 |
Public Members:
• A constructor to initialize the data members.
• A function InputData() to allow the user to enter the values
• A function displaydata() to display all and call getdata function
Ans:
Python3
class train : def __init__( self ): _src = "" _tnm = "" _dest = "" _charges = 0.0 def getdata( self ): if self ._dest = = "mumbai" or self ._dest = = "MUMBAI": self ._charges = 1000 elif self ._dest = = "chennai" or self ._dest = = "CHENNAI" : self ._charges = 2000 elif self ._dest = = "kolkata" or self ._dest = = "KOLKATA" : self ._charges = 2500 def inputdata( self ): self ._src = raw_input ("enter the source of journey") self ._tnm = raw_input ("enter the train name") self ._dest = raw_input ("enter the destination") |
d. Observe the following class definition and answer the question that follow: 2