Final_Questions_for_OOPD_23M____Rubrics
Final_Questions_for_OOPD_23M____Rubrics
Roll Number:
Stream:
A2 Consider a case where a user has entered a wrong input, but the program’s logic has enough intelligence
to work in spite of getting a wrong input. Which technique of C++ is the best to handle such a case?
1
(a) Yes, possible. – 1 mark
(b) By using a comparator function / by using operator overloading – 2 marks
A4 Consider the case of a static variable x declared outside any functions. Functions f 1 and f 2 are defined
in files F 1 and F 2 respectively. The functions f 1 and f 2 both manipulate x in different threads. Is
there a possibility of a race condition? Justify.
A6 Consider a program that requires both gcc-7 and python 3.6. A user decides to install a virtual machine
(VM) first, and then install the program within the VM. Is this a good decision, and why or why not?
(a) No, it is a wrong decision – 1 mark
(b) It consumes too much disk space, and introduces overhead of execution – 2 marks
A7 Consider two disks with the same filesystem on the same machine, with the home directories of users
divided among them. A user finds his own disk full, and decides to utilize the installation of Matlab
by another user on the other disk by using a hard link. Is this a good strategy? Justify.
(a) No, it is a wrong strategy – 1 mark
(b) Hard links do not work across filesystems – 2 marks
A8 Suppose you find that your program has a bug in it. You fix the code, save the file and then re-compile
the program. Is it possible that you again encounter the same bug in the executable file? If so, why
and how can it be prevented?
(a) Yes, it is possible – 1 mark
(b) The changes might be stored on the disk cache and not be actually written – 1 mark
(c) Prevention – Use synchronized write / sync command – 1 mark
A9 Consider a program p.out which generates too much amount of text, but only a limited amount of it
is useful. The useful lines are always marked with a “INFO: ” tag. What is the best way to store only
these lines into a file while ignoring the other lines of output? You must mention the exact command.
./p.out — grep ”INFO:” ¿ output
(a) 1 mark for pipe and redirection
(b) 1 mark for using grep
(c) 1 mark for executing p.out and writing output filename
A10 Consider the case of a filesystem where a large file was being written. The inodes were allocated, but
before the allocated inodes could be registered in the filesystem, power went off. Would it be possible
to recover the data that was written on restarting the machine? If so, how? You may assume that no
other records about the existence of the inodes exist anywhere on the filesystem.
(a) Yes, it is possible – 1 mark
(b) By running a disk check / using fsck command – 2 marks
2
A11 Consider the following program that uses dynamic memory allocation:
i n t main ( v o i d ) {
i n t ∗x = NULL;
x = malloc ( 4 0 ) ;
x [ 9 ] = 5;
return 0;
}
What is the content of the array that was allocated when the return statement was invoked? <9
garbage values, 5>
(a) 1 mark for specifying 10 elements
(b) 1 mark for specifying 9 garbage values
(c) 1 mark for specifying ”5” at the end
A12 Suppose you utilize a vector of vectors to store a 2-dimensional matrix. Now, we decide to fill up the
rows by using the append function. Is this a good idea? Justify.
(a) No, not a good idea – 1 mark
(b) Repeated allocations of memory lead to poor performance – 2 marks
A13 Suppose a public function of a derived class overrides one (defined as virtual) in the base class. Is it
possible for the derived class function to utilize the function of base class?
(a) Yes, it is possible – 1 mark
(b) Need to use BaseClass::function() from within the derived class – 1 mark for specifying the scope
operator + 1 mark for specifying the right syntax
A14 Suppose you utilize gprof to profile the C++ code. Since C++ code is compiled into object files
and linked into executable file, is it still possible for gprof to know which line of (C++) source code
consumes how much time? If so, how? If not so, what does gprof actually show?
(a) Yes, it is still possible –1 mark
(b) During compilation, the ”-g” symbol adds additional data about the source code during compila-
tion – 2 marks for specifying using either ”-g” symbol or adding data during compilation
A15 What will be the output of the following program?
class C {
public :
int x ;
C( ) { x = 0 ; }
void setValue ( i n t x ) { x = 5 ; }
};
i n t main ( v o i d ) {
C c;
c . setValue ( 3 ) ;
c o u t << c . x ;
return 0;
}
3
2 Long Questions (5 x 6 = 30 points)
B1 The Indian Railways runs a variety of trains, falling into a variety of categories. The highest level of
category are the passenger and freight trains. The passenger trains are categorized as mail and express
trains, among which a limited number are also sub-categorized as superfast trains. Superfast trains
also have the attribute that they more passenger amenities than non-superfast trains. Assuming that
the train signaling system requires assigning priority based on the sub-category of trains, which is kept
track using a priority parameter. Design a class structure that can automatically compute the priority
of trains, depending on their type. You may assume that the highest priority is given to superfast,
followed by express but non-superfast, followed by mail trains, followed by freight trains.
c l a s s Train {
virtual int getPriority () = 0;
};
c l a s s P a s s e n g e r T r a i n : p u b l i c Train {
int passengerAmenities ;
virtual int getPriority () { return 2; }
};
c l a s s F r e i g h t T r a i n : p u b l i c Train {
virtual int getPriority () { return 1; }
};
c l a s s ExpressTrain : public PassengerTrain {
virtual int getPriority () { return 3; }
};
c l a s s SuperFastTrain : p u b l i c ExpressTrain {
virtual int getPriority () { return 4; }
};
B2 (a) Consider a program in which there are two vectors A and B on which Fast Fourier Transform
needs to be performed. The operation on A takes some tp1 time and on B takes some tp2 time, in
Python. However, the programmer decides to create threads so that they can be run concurrently.
What is the approximate running time of this program, ignoring other overheads of creating the
threads?
i. The sum of the two times – 1 mark
ii. Python does not allow running of parallel threads – 1 mark
(b) Assume that there are two threads that run concurrently which increases the value of a global
variable x. If x was initialized to 0, what is the final value of x, if the program is written in (i)
C++, and in (ii) Python?
i. In C++, value is not deterministic – 1 or 2 – 1 mark for saying either of them
ii. In Python, the value will be 2 – 1 mark
iii. C++ allows running parallel threads, but Python does not – 1 mark
B3 You want to create an abstract data structure that has a key and object. Search and sort operations
always happen only by referring to the key, but the object contains other essential information. Explain
and justify the best solution in the following cases:
(a) The data type of the key can vary depending on the use case.
(b) The data type of the object can vary depending on the use case.
4
(a) Using a template is best, with operator overloading, because the comparison operation is a must
–1+1+1
(b) Either templates or void pointer (Any one) – 2 marks
B4 Consider two programs P1 and P2 both of which implement sorting of arrays of ints and floats. P1
uses templates to do the sorting, whereas P2 uses function overloading to write the two functions.
Which among P1 and P2 have higher compilation time, and specify that who among P1’s int sort,
P1’s float sort, P2’s int sort and P2’s float sort are the fastest and slowest respectively? You need to
justify the reason.
(a) P1 has higher compilation time, because templates make compilation slower – 1 + 1
(b) P1’s float sort and P2’s float sort both are equally slow (and similarly P1’s int sort and P2’s int
sort both are equally fast) – 3 marks (No marks for saying only one among P1’s float or P2’s
float)
B5 The Indian immigration system classifies countries into trusted and distrusted categories. Citizens of
trusted countries get a visa on arrival, whereas citizens of distrusted countries do not get. There are
also different categories of visa, such as tourist, work, education. Note that every visa must belong to
some category. Furthermore, anybody making a false claim is noted down, and further visa requests
are denied to them. Design a class structure to model the visas issued by the immigration system.
class Citizen {
};
c l a s s TrustedCountryCitizen : public Citizen {
bool visaOnArrival () { return true ; }
c l a s s DistrustedCountryCitizen : public Citizen {
bool visaOnArrival () { return f a l s e ; }
c l a s s Visa ( ) {
Citizen c ;
public v i r t u a l bool issueVisa () = 0;
};
c l a s s T o u r i s t V i s a : p u b l i c Visa {
p u b l i c v i r t u a l b o o l i s s u e V i s a ( ) { /∗ . . . ∗/ }
};
c l a s s WorkVisa : p u b l i c Visa {
p u b l i c v i r t u a l b o o l i s s u e V i s a ( ) { /∗ . . . ∗/ }
};
c l a s s EduVisa : p u b l i c Visa {
p u b l i c v i r t u a l b o o l i s s u e V i s a ( ) { /∗ . . . ∗/ }
};
B6 In the above question, the application is considered critical, as a decision has major impact on the
security. This requires unit testing of various classes that deal with issuing of visas. How would you
go about testing each of such classes? You need to show the structure of any extra code that might be
needed.