Professional Documents
Culture Documents
OOP 1
TABLE OF CONTENTS
PHYSICS 1: OBJECT ORIENTED PROGRAMMING.
PRELIM
Chapter 1: Introduction of OOP
Activity 1..........................................................................................................................1
MIDTERM
CHAPTER 5: M a n a g i n g c o n s o l e i n p u t / o u t p u t o p e r a t i o n s
Activity 5...........................................................................................................................10
FINALS
CHAPTER 8: Template
Activity 8................................................................................................................. 19
OBJECT ORIENTED
OOP 2
OBJECTIVES:
ACTIVITY 1.1
Answer the following Questions
1. When was the time that many software project failed?(answer 1960’s)
2. This ambitious project was undertaken by IBM to create a single operating system
PROJECT (1963-1965)
3. the most chilling software failure in the history of Computer Science (Answer The
therac-25
4. was designed to be the most advanced baggage system in the world (Answer The
OBJECT ORIENTED
OOP 3
5. This was the sad culmination of a decade-long project costing $7 billion ( Answer
Ariane 5 rocket )
6. This project was slated to open in August 2003 (Answer German Toll Collect
System)
spectre exploits)
OBJECT ORIENTED
OOP 4
ACTIVITY 1.2
Fill in the blank
1. _______restricting the growth of computing field due to huge gap between hardware developments
and making use of it through non availability of software systems and competent software development
staff.( Answer Abstract software Crisis)
2. This term was given by F. L. Bauer at the first NATO Software Engineering Conference
in_________( Answer 1968)
3. During the 1990's, the cost of ownership and maintenance increased by_____(Answer 30% over
the 1980's)
4. The average software project overshoots its schedule by_______(Answer half)
5. Project planning means______(Answer creating work breakdown)
ACTIVITY 1.3
OBJECT ORIENTED
OOP 5
Functions are the building blocks of C++ programs where all the program activity occurs. Function is a
collection of declarations and statements. .
ACTIVITY 2.1
2) How are the argument data types specified for a C++ function? Explain with
Suitable example.
4) What is recursion? While writing any recursive function what thing(s) must be taken care of ?
5) What is inline function? When will you make a function inline and why ?
OBJECT ORIENTED
OOP 6
ACTIVITY 2.2
ACTIVITY 2.3
2. ________the data that the function receives when called/invoked from another function. (ANSWER
Arguments of a function)
3. C ++ allows a function to assign a parameter the default value in case no argument for that
parameter is specified in the function call. (ANSWER: Default Arguments)
4._________A C++ function may have constant arguments(s). These arguments(s) is/are treated
as constant(s). These values cannot be modified by the function. (ANSWER: Constant Argument)
5._________In this method the values of the actual parameters (appearing in the
function call) are copied into the formal parameters (appearing in the function definition), i.e., the
function creates its own copy of argument values and operates on them. (ANSWER: Call by value)
OBJECT ORIENTED
OOP 7
A constructor (having the same name as that of the class) is a member function which
is automatically used to initialize the objects of the class type with legal initial values.
Destructors are the functions that are complimentary to constructors. These are used to deinitialize
objects when they are destroyed. A destructor is called when an object of the class
goes out of scope, or when the memory space used by it is de allocated with the help of delete
operator.
ACTIVITY 3.1
OBJECT ORIENTED
OOP 8
3.Which of the following operators are overloaded by default by the compiler in every user defined
classes even if user has not written?
1) Comparison Operator ( == )
2) Assignment Operator ( = )
Both 1 and 2
A
Only 1
B
Only 2 (Answer)
C
None of the two
D
4.Which of the following operators should be preferred to overload as a global function rather than a
member method?
Postfix ++
A
Comparison Operator
B
Insertion Operator << (Answer)
C
Prefix++
D
5.How does C++ compiler differs between overloaded postfix and prefix operators?
C++ doesn't allow both operators to be overloaded in a class
A
A postfix ++ has a dummy parameter (Answer)
B
A prefix ++ has a dummy parameter
C
By making prefix ++ as a global function and postfix as a member function.
D
OBJECT ORIENTED
OOP 9
ACTIVITY 3.2
Direction: Run the following program, and choose the correct answer Below. (20 pts.)
class B
{
int x;
public:
B(int xx) : x(xx) {}
operator A() const { return A(x); }
};
void g(A a)
{
a.show();
}
int main()
{
B b(10);
g(b);
g(20);
return 0;
}
Run on IDE
Compiler Error
A
10 (Answer)
B 20 (Answer)
20
C 20
10
D 10
OBJECT ORIENTED
OOP 1
ACTIVITY 3. 3
Direction: Run the following program, and choose the correct answer Below. (20 pts.)
class Test
{
int x;
Test2 t2;
public:
operator Test2 () { return t2; }
operator int () { return x; }
};
int main()
{
Test t;
fun(t);
return 0;
}
Run on IDE
fun(int) called
A
fun(Test 2) called
B
Compiler Error: Ambiguous call to fun() (Answer)
C
OBJECT ORIENTED
OOP 1
INTRODUCTION
A virtual function is a member function which is declared in the base class
using the keyword virtual and is re-defined (Overriden) by the derived class.
The term Polymorphism means the ability to take many forms. It occurs if
there is a hierarchy of classes which are all related to each other by
inheritance
ACTIVITY 4.1
Direction: Run the following program, and choose the correct answer Below. (20 pts.)
Predict output of the following program
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show();
return 0;
}
OBJECT ORIENTED
OOP 1
Run on IDE
In Base
A In Base
In Base
B In Derived
In Derived (Answer)
C In Derived (Answer)
In Derived
D In Base
ACTIVITY 4.2
OBJECT ORIENTED
OOP 1
A. private
B. Protected (Answer)
C. public
ACTIVITY 4.3
Direction: Run the following program, and choose the correct answer Below. (20 pts.)
public:
Mammal(){
cout<<"I'm a Mammal\n";
~Mammal(){}};
public:
Human(){
cout<<"I'm a Human\n";
~Human(){}};
OBJECT ORIENTED
OOP 1
class Male: public Human{
public:
Male(){
cout<<"I'm a Male\n";
~Male(){}};
public:
Female(){
cout<<"I'm a Female\n";
~Female(){}};
Male M;
return 0;}
OBJECT ORIENTED
OOP 1
C++ supports two complete I/O systems: the one inherited from C and the object
oriented I/O system defined by C++ (hereafter called simply the C++ I/O system). Like
C-based I/O, C++'s I/O system is fully integrated. The different aspects of C++'s I/O
system, such as console I/O and disk I/O, are actually just different perspectives on the
same mechanism.Every program takes some data as input and generates processed
data as output following the input-process-output cycle.C++ supports all of C’s rich set
of I/O functions that can be used in the C++ programs.But these are restrained from
using due to two reasons ,first I/O methods in C++ supports the concept of OOP and
secondly I/O methods in c can not handle the user defined data types such as class
objects.C++ uses the concept of streams and stream classes to implement its I/O
operation with the console and disk fils..
ACTIVITY 5.1
Matching type :
Put the right response in the box provided.
E.Streambuf
OBJECT ORIENTED
OOP 1
ACTIVITY 5.2
Answer the following questions ,Choose the letter of the correct answer
1. Which header file is used with input and output operations of C in C++?
A. stdio.h
B. Cstdio (Answer)
C. iostream
D. Streamio
2.Which will be used with physical devices to interact from C++ program?
A. Programs
B. Library
C. Streams (Answer)
D. Iterators
A. 1
B. 2
C. 3 (Answer)
D. 4
4.What is the benefit of c++ input and output over c input and output?
A. Type safety ( Answer)
B. Exception
C. Both Type safety & Exception
D. Sequence container
OBJECT ORIENTED
OOP 1
ACTIVITY 5.3
Direction: Run the following program, and choose the correct answer Below. (20 pts.)
What is the name of the myfile2 file after executing the following C++ code?
1.
#include <stdio.h>
2.
3.
int main ()
4.
5.
{
6.
7.
int result;
8.
9.
char oldname[] = "myfile2.txt";
10.
11.
char newname[] = "newname.txt";
12.
13.
result = rename(oldname, newname );
14.
15.
if (result == 0)
16.
17.
puts ( "success" );
18.
19.
else
20.
21.
OBJECT ORIENTED
OOP 1
perror( "Error" );
22.
23.
return 0;
24.
25.
}
26.
A. name
B. new
C. newname (Answer)
D. Error
OBJECT ORIENTED
OOP 1
OBJECTIVES:
ACTIVITY 6.1
1.By default, all the files in C++ are opened in _______ mode.
A) Text (Answer)
B) Binary
C) ISCII
D) VTC
3.Which of the following is the default mode of the opening using the ofstream class?
A) ios::in
B) ios::out (Answer)
C) ios::app
D) ios::trunc
OBJECT ORIENTED
OOP 2
8-10 Write a C++ program to write number 1 to 100 in a data file NOTES.TXT
(Answer)
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("NOTES.TXT");
for(int i=1;i<=100;i++)
fout<<i<<endl;
fout.close();
return 0;
}
OBJECT ORIENTED
OOP 2
ACTIVITY 6.2
DIRECTION: INSERT ALL THE CORRECT ANSWER BELLOW
PARAMETER MEANING
A (answer) Append to end-of-file
B (answer) Go to end-of-file on opening
C (answer) Binary file
D (answer) Open file for reading only
E (answer) Open fails if file the file does not exist
F (answer) Open fails if file the file does not exist
G (answer) Open file for writing only
H (answer) Delete the contents of the file if it exists
A. ios::app
B. ios::ate
C. ios::binary
D. ios::in
E. ios::nocreate
F. ios::noreplace
G. ios::out
H. ios::trunk
OBJECT ORIENTED
OOP 2
ACTIVITY 6.3
3. What is a file mode ?describe the various file mode options available.
4.Describes the various approaches by which we can detect the end of file condition.
OBJECT ORIENTED
OOP 2
INTRODUCTION:
Software engineers have been trying various tools, methods, and procedures to control
the process of software development in order to build high quality software with
improved productivity. The methods provide “how to s” for building the software while
the tools provide automated or semi-automated support for the methods. They are used
in
all the stages of software development process, namely, planning, analysis, design,
development and maintenance. The software development procedures integrate the
methods and tools together and enable rational and timely development of software
systems. They provide guidelines as to apply the methods and tools, how to produce
the
deliverables at each stage, what controls to apply, and what milestones to use to
assess
the progress.
ACTIVITY 7.1
1._________ covers a detailed study of the requirements of both the user and the
Software (Answer:ANALYSIS)
4._____________may involve the individual units and the whole systems. It requires a
detailed plan as to what, when and how to test. (Answer: TESTING)
6. The _______ developed in the 1960’s and 1970’s are called the traditional tools.
(Answer:FIRST GENERATION TOOLS)
8.The recent tools are the __________ones evolved since late 1980’s to suit the object-
oriented analysis and design.(Answer: THIRD GENERATION)
OBJECT ORIENTED
OOP 2
ACTIVITY 7. 2
6. A diagram showing the inputs and their sources and the outputs and
their destinations. (F)
11. A graphic representation of the condition and outcomes that resemble the
branches of a tree.(K)
.
A.System flowcharts
B.Play script:
C.Program flowcharts:
D.Layout forms:
E.Grid charts
F.Context diagrams:
G.Data flow diagrams:
OBJECT ORIENTED
OOP 2
H.Structure chart
I.Data dictionary:
J.Decision table:
K.Decision tree:
ACTIVITY 7.3
OBJECT ORIENTED
OOP 2
CHAPTER 8: TEMPLATE
OBJECTIVES:
INTRODUCTION
Template is a new concept which enables us to define generic and functions and thus
provides support for generic programming. Generic programming as an approach where
generic types are used as parameters in algorithms so that they work for a variety of
suitable data types and data structures.
A template can be used to create a family of classes or functions. For example, a class
template for an array class would enable us to create arrays of various data types such as
int array and float array .similarly, we can define a template for a function, say mul(),hat
would help us create versions of mul() for multiplying int, float and double type values.
A template can be considered as a kind of macro. When an object of a specific type is
define for actual use, the template definition for that class is substitute with the required
data type. Since a template is defined with a parameter that would be replaced by a
specified data type at the time of actual use of the class or function, the templates are
sometimes called parameterized class or functions.
ACTIVITY 8.1
2) We can write one function that can be used for all data types including user defined types. Like
sort(), max(), min(), ..etc.
3) We can write one class or struct that can be used for all data types including user defined types. Like
Linked List, Stack, Queue ..etc.
4) Template is an example of compile time polymorphism.
1 and 2
A
1, 2 and 3
B
1, 2 and 4
C
1, 2, 3 and 4 (ANSWER)
D
OBJECT ORIENTED
OOP 2
2.Which of the following provides the best description of an entity type?
A specific concrete object with a defined set of processes (e.g. Jatin with
A diabetes)
A value given to a particular attribute (e.g. height - 230 cm)
B
A thing that we wish to collect data about zero or more, possibly real world
C examples of it may exist
A template for a group of things with the same set of characteristics that may
D exist in the real world (ANSWER)
int main()
{
fun<int> (1);
cout << endl;
fun<int>(1);
cout << endl;
fun<double>(1.1);
cout << endl;
return 0;
}
Run on IDE
OBJECT ORIENTED
OOP 2
x = 1 count = 0
x = 1 count = 1 (ANSWER)
A
x = 1.1 count = 0
x = 1 count = 0
x = 1 count = 0
B
x = 1.1 count = 0
x = 1 count = 0
x = 1 count = 1
C
x = 1.1 count = 2
Compiler Error
D
template<class T>
int Test<T>::count = 0;
int main()
{
Test<int> a;
Test<int> b;
Test<double> c;
cout << Test<int>::count << endl;
cout << Test<double>::count << endl;
return 0;
}
OBJECT ORIENTED
OOP 3
Run n IDE
0
A 0
1
B 1
2 (ANSWER)
C 1
1
D 0
ACTIVITY 8.2
1-5.Output of following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and
there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
A<char, char> a;
A<int, int> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
Run on IDE
6
A 12
OBJECT ORIENTED
OOP 3
2 (ANSWER)
B 8
Compiler Error: There can not be more than one template arguments.
C
8
D 8
5-10.Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes,
and there is no alignment done by the compiler.
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
A<int, int> a;
A<double, double> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
Run on IDE
16
A 24 (ANSWER)
8
B 16
20
C 28
ACTIVITY 8.3
OBJECT ORIENTED
OOP 3
4-10.Output of following program? Assume that the size of char is 1 byte and size of int is 4 bytes, and
there is no alignment done by the compiler.
OBJECT ORIENTED
OOP 3
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
A<char, char> a;
A<int, int> b;
cout << sizeof(a) << endl;
cout << sizeof(b) << endl;
return 0;
}
Run on IDE
6
A 12
2
B 8 (ANSWER)
Compiler Error: There can not be more than one template arguments.
C
8
D 8
OBJECT ORIENTED
OOP 3
CHAPTER 9: EXCEPTION HANDLING
OBJECTIVES:
Explain the mechanism of exception handling
Describe the role of keywords try,throw and catch in exception handling
INTRODUCTION
Usually there are mainly two type of bugs, logical errors and syntactic errors. The
logical errors occur due to poor understanding of problem and syntactic errors arise due
to poor understanding of language. There are some other problems called exceptions that
are run time anomalies or unused conditions that a program may encounter while
executing. These anomalies can be division by zero,access to an array outside of its
bounds or running out of memory or disk space. When a program encounters an
exceptional condition it is important to identify it and dealt with it effectively.An
exception is an object that is sent from the part of the program where an error occurs to
that part of program which is going to control the error
ACTIVITY 9.1
Direction: Run the following program, and choose the correct answer Below.
try{
}catch(Exception e){
System.out.println("Test Exception");
}
}
A. Yes (ANSWER)
B. No
4-6.What will happen when you compile and run the following code?
int a = 5, b = 3;
try{
if( a % b > 0 )
throw new Test();
}catch(Exception e){
System.out.println("Test Exception");
}
}
}
A. No output
B. Test Exception
C. Run time error
D. Compilation error (ANSWER)
6-10.What will happen when you compile and run the following code?
public class Test{
try{
}catch(Exception e){
System.out.print("Exception 1 ");
}
}
int c = 0;
try{
c = a * b;
if(c > 10)
throw new String("Cannot be more than 10 ");
OBJECT ORIENTED
OOP 3
}catch(Exception e){
System.out.print("Exception 2 ");
}
return c;
}
A. Exception 2
B. Exception 2 Exception1
C. 10
D. Compilation error (ANSWER)
ACTIVITY 9.2
Direction: Run the following program, and choose the correct answer Below.
1-3. What will happen when you compile and run the following code?
String className;
try{
}catch(Exception e){
System.out.print("Exception ");
}catch(NullPointerException ne){
System.out.print("Null ");
}
A. Test
OBJECT ORIENTED
OOP 3
B. Exception
C. Null
D. Compilation error (ANSWER)
4.Runtime Exception and its sub-classes can be caught by the catch block.
A. True (ANSWER)
B. False
5-10.What will happen when you compile and run the following code?
try{
}catch(NameIsJohnException e){
System.out.print("NameIsJohnException ");
}
}
class NameIsJohnException{
String message;
public NameIsJohnException(){
}
public NameIsJohnException(String message){
this.message = message;
}
}
OBJECT ORIENTED
OOP 3
ACTIVITY 9.3
Direction: Run the following program, and choose the correct answer Below.
1-5. What will happen when you compile and run the following code?
import java.io.IOException;
try{
}catch(IOException e){
System.out.println("Exception1");
}
}
OBJECT ORIENTED
OOP 3
private void doNothing(){
for(int i = 0 ; i < 10; i++){
}
}
}
A. No output
B. I have done nothing
C. Exception1
D. Compilation error (ANSWER)
6-10. What will happen when you compile and run the following code?
public Test(){}
public Test(String str){
super(str);
}
int importantData = 5;
public static void main(String[] args){
Test t = new Test();
t.importantMethod();
}
A. No output
B. 5
C. Exception - Important data is invalid
D. Compilation error (ANSWER)
OBJECT ORIENTED
OOP 4
OBJECTIVES:
Describes bundling data and methods that work on that data within one unit, like
a class in Java.
INTRODUCTION
ACTIVITY 10.1
MULTIPLE CHOICE
4.Which one of the following are essential features of object oriented language? A. Abstraction and
encapsulation B. Strictly-typed C. Type-safe property coupled with sub-type rule D. Polymorphism in
the presence of inheritance
A. .A and B only
B. A, D and B only
C. A and D only (Answer)
D. A, C and D only
OBJECT ORIENTED
OOP 4
5. Abstraction and encapsulation are fundamental principles that underlie the object oriented approach
to software development. What can you say about the following two statements ? I. Abstraction allows
us to focus on what something does without considering the complexities of how it works. II.
Encapsulation allows us to consider complex ideas while ignoring irrelevant detail that would confuse
us.
ACTIVITY 10.2
Each question is worth Two points. Select the best answer for each question
The keyword private restricts the access of class or struct members to:
1.
Please select the best answer.
A
const functions
.
B
static functions
.
C. member functions (ANSWER)
D. clients
OBJECT ORIENTED
OOP 4
If we have
class card {
public:
int s;
};
3.
card a;
card* p;
we can use _________ to access int s:
Please select the best answer.
A
p.s
.
B
a.s (ANSWER)
.
C. p -> int
D. a -> s
In object-oriented software design, there is at least one extra step before you get to the
4. coding of algorithms. That step is the design of:
Please select the best answer.
A
classes that are appropriate for the problem at hand (ANSWER)
.
B
member functions
.
C. program control flow
D. the class's interface
An interface in C++ is a
5.
Please select the best answer.
A
program that allows easy conversion of another program's data
.
B
list of argument types in a function's parameter list
.
C. library for performing input and output
D. class's public members (ANSWER)
OBJECT ORIENTED
OOP 4
ACTIVITY 10.3
A. Objects
B. Methods
C. Classes (ANSWER)
A. Objects (ANSWER)
B. Methods
C. Classes
B. Data Hidding
C. Data Encapsulation
OBJECT ORIENTED
OOP 4
A. public
B. Private (ANSWER)
C. static
D. class
5.The ability to define more than one function with the same name is called?
A. Inheritance
B. Encapsulation
C. Polymorphism (ANSWER)
D. Abstraction
OBJECT ORIENTED