short c++ question with answer for beginners, Exams of Computer Science

question with answer in specified format for beginners student capsul for exam

Typology: Exams

2017/2018

Uploaded on 11/15/2018

490412asdf
490412asdf 🇳🇵

1 document

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2203 Object Oriented Programming
2 Marks Questions & Answers
Basics of OOPs
1. List out the characteristics of FOP.
1. Large programs are divided into smaller programs known as functions.
2. Most of the functions share global data
3. Functions transform data from one form to another.
4. It employs top-down approach.
2. List out the characteristics of OOP.
1. Programs are divided into objects.
2. Data is hidden.
3. Objects communicate with each other, by sending messages and receiving responses.
4. It follows bottom-up approach.
3. List down the basic concepts of OOP.
1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
4. Define an object.
Objects are the basic run time entities in an object oriented system.
They may represent a person, a place or any item that a program has to handle.
5. Define Object Oriented Programming.
OOP is a method of implementation in which programs are organized as co-operative collection of
objects, each of which represents an instance of some class and whose classes are all members
of a hierarchy of classes united through the property called Inheritance.
6. Define a class.
A class is a collection of objects with similar attributes and operations.
Eg. Class – Account
It will create an object savings_account belonging to the class Account.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download short c++ question with answer for beginners and more Exams Computer Science in PDF only on Docsity!

CS 2203 Object Oriented Programming

2 Marks Questions & Answers

Basics of OOPs

1. List out the characteristics of FOP.

  1. Large programs are divided into smaller programs known as functions.
  2. Most of the functions share global data
  3. Functions transform data from one form to another.
  4. It employs top-down approach. 2. List out the characteristics of OOP.
  5. Programs are divided into objects.
  6. Data is hidden.
  7. Objects communicate with each other, by sending messages and receiving responses.
  8. It follows bottom-up approach. 3. List down the basic concepts of OOP.
  9. Objects
  10. Classes
  11. Data abstraction and encapsulation
  12. Inheritance
  13. Polymorphism
  14. Dynamic binding
  15. Message passing 4. Define an object. Objects are the basic run time entities in an object oriented system. They may represent a person, a place or any item that a program has to handle. 5. Define Object Oriented Programming. OOP is a method of implementation in which programs are organized as co-operative collection of objects, each of which represents an instance of some class and whose classes are all members of a hierarchy of classes united through the property called Inheritance. 6. Define a class. A class is a collection of objects with similar attributes and operations. Eg. Class – Account It will create an object savings_account belonging to the class Account.

7. Define Encapsulation. The wrapping up of data and functions into a single unit is known as encapsulation. The data is kept safe from external interference and misuse. 8. Define Data hiding? The isolation of data from direct access by the program is called as data hiding or information hiding. 9. Define Abstraction. It refers to the act of representing essential features without including the background details. 10. Define ADT? The classes which are using the concept of data abstraction is known as abstract data types (ADT). 11. Define data member and member function? The attributes which are holding the information is known as data members. The functions that operate on data member are sometimes called as member function. 12. Define Inheritance? Inheritance is the process by which objects of one class acquire the properties of objects of another class. The new derived class inherits the members of the base class and also adds its own. 13. Define Polymorphism? It allows a single name/operator to be associated with different operations depending on the type of data passed to it. An operation may exhibit different behaviors in different instances. 14. Define dynamic binding? Dynamic binding means that the code associated with the given procedure call is not known until the time of call at runtime. 15. What is message passing? It is the process of invoking an operation on an object. In response to a message, the corresponding function is executed in the object. 16. List down the benefits of OOP?

  1. The principle of data hiding helps to build secure programs.
  2. Through inheritance, redundant code is eliminated.

23. List the different types of parameter passing techniques. (i) Pass by value (ii) Pass by Address (iii) Pass by Reference 24. What is Dynamic memory allocation? Allocation of memory space for any data structure during the course of the program execution is called as Dynamic memory allocation. 25. How memory management is performed dynamically in C++? Two operators are available for dynamic memory management. (i) new – for dynamic memory allocation (ii) delete – for dynamic memory deallocation

Basics of C++ Language

1. Define class? A class is a way to bind data and its associated functions together. It allows the data to be hidden if necessary. 2. What are the parts of class specification? The class specification has two parts

  1. Class declaration – To describe the type and scope of its members
  2. Class function definition – To describe how the class functions are implemented. 3. Write the syntax of class declaration class classname { Private: Variable declaration; Function declaration; Public: Variable declaration; Function declaration; }; 4. Where will you define a member function? Member functions can be defined in two places.
  1. Outside the class definition.
  2. Inside the class definition. 5. Give the syntax for member function definition outside the class. Return type class name:: function name (argument name declaration) { function body } 6. List the characteristics of member function.
  3. Member function can access the private data of class.
  4. A member function can call another member function directly without using the dot operator. 7. Define nesting of member function. A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions. 8. List the properties of static members. A data member of a class can be qualified as static properties.
  5. It is always initialized to zero when the first object of its class is created.
  6. It is visible only within the class. 9. Write the properties of static member function.
  7. A static function can have access to only other static members declared in the same class.
  8. A static member function can be called using the class name (Instead of objects) as follows: class name:: function name; 10. Define constructor? A constructor is a special member function whose task is to initialize the object of its class. It is called constructor because it constructs the value of data members of the class. 11. What are the characteristics of constructor?
  9. Constructors should be declared in public section.
  10. They are involved automatically when the objects are created.
  11. They do not have return types.
  12. They can not be inherited. 12. Define parameterized constructor. Arguments can be passed to the constructor function when the objects are created. The constructors that can take arguments are called as parameterized constructor.
  1. Data move openly around the system from function to function
  2. Functions transform data from one form to another
  3. Uses top-down approach in program design.
  4. Concentration is on doing things( algorithms) 2.What are the features of object-oriented programming languages?
  5. Programs are divided into objects
  6. Data structures designed such that they characterize the objects.
  7. Functions that operate on the data of an object are tied together in the data structure
  8. Data is hidden and cannot be accessed by external functions
  9. Objects may communicate with each other through functions
  10. New data and functions can be easily added whenever necessary
  11. IT follows bottom-up approach in program design
  12. Concentration is on data rather than procedure 3.Define Object Oriented Programming It is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand.
  13. What is meant by an object?  Objects are the basic run-time entities in an object-oriented system.  They may represent a person, a place, , a table of data or any item that the program must handle.  They may also represent user defined data such as vectors, time and lists.  When a program is executed, the objects interact by sending message to one another.  Each object contains data and code to manipulate the data.  Objects can interact without knowing having to know details of each other’s data or code. 5. What is meant by Classes?
  14. A class is a collection of objects of same type.
  15. Once the class has been defined , we can create any number of objects belonging to that class.
  16. Each object is associated with the data of type class with which they are created. 6.What is meant by Encapsulation?  The wrapping up of data and functions into a single unit ( called class) is known as encapsulation  Data encapsulation is the most striking feature of a class.  The data is not accessible to the outside world and only those functions which are wrapped

in the class can access it.  These functions provide the interface between the object’s data and the program  This insulation of the data from direct access by the program is called data hiding.

  1. What is meant by Abstraction  => It refers to the act of representing essential features without including the background details or explanation  Classes use the concept of abstraction and are defined as a list of abstraction and are defined as a list of abstract attributes and functions to operate on these attributes.  They encapsulate all the essential properties of the objects that are to be created 8. Define Inheritance.  Inheritance is the process by which objects of one class acquire the properties of objects of another class.  It provides the idea of reusability.  This is possible by deriving a new class from the existing class. The new class will have the combined features of both the classes. 9.Define polymorphism  It means the ability to take more than one form.  For ex, consider the operation of addition.  If the operands are strings, then the operation would produce a third string by concatenation.  If the operands are numbers, it will generate a sum. 10.Define dynamic binding  It refers to the linking of a procedure call to the code to be executed in response to the call.  Dynamic binging means that the code associated with a given procedure call is not known until the time of the call at run time.  It is associated with polymorphism and inheritance 11. What r the benefits of OOP  Through inheritance, we can eliminate redundant code and extend the use of existing classes.  It saves the program development time and higher productivity.  The use of data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program  It is possible to have multiple instances of an objects to co-exist without any interference.  It is possible to map objects in the program domain to those objects in the program.

 It allows the programmer to build large programs with clarity, extensibility and ease of maintenance, incorporating the sprit and efficiency of C

15. What are the applications of C++?  It is a versatile language for handling very large programs  It is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real life application systems  It allows us to create hierarchy-related objects, so we can build special object-oriented libraries which can be used later by many programmers  While C++ is able to map the real-world problem properly, the C part of C++ gives the language the ability to get close to the machine – level details  C++ programs are easily maintainable and expandable 16. Why we need the preprocessor directive #include?  This directive causes the preprocessor to add the content of the iostream file to the program. It contains declarations for the identifier cout and the operator <<.  It should be included at the beginning of all programs that use i/o statements. 17. How does a main() function in C++ differ from main() in C? In C++, main() returns an integer type value to the operating system. Therefore , every main() in C++ should end with a return (0) statement. Otherwise a warning or an error occurs. Since main() returns an integer type value, return type for main() is explicitly specified as int. In C, it does not specify any return value for the main() function. So there is no need for explicitly use the return statement. 18. Describe major parts of a C++ program.


| Include File | | ——————————– | | Class Declaration | | ——————————-| | Member Function Definition | | ——————————-| | Main Function Program | |______________________| The class declarations are placed in a header file and the definitions of member functions go into another file. The main program that uses the class is placed in a third file which “ includes” the previous two files as well as any other files required. 19. Describe I/O operator.

Input Operator The statement cin>> num; is an input statement and causes the program to wait for the user to type in a number. The operator >> is known as extraction or get from operator. It takes the value from the keyboard and assigns it to the variable on its right. Output Operator The statement Cout<< “ the numbers”; uses the cout identifier that represents the standard output stream ( screen) in C++. The operator <

20. Describe tokens The smallest individual units in a program are known as tokens. C++ has the following tokens: 1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Operators. 21. Classifications of Data Types. 1. User defined Type a. Structure b. Union c. Class d. Enumeration 2. Built in type a. integral type i. int ii. char b. void c. floating type i. float ii. double d. Derived type i. array ii. function iii. pointer iv. reference 22.Describe with example the uses of enumeration data type (i) It provides a way for attaching names to numbers. (ii) The enum keyword gives the list of words by assigning them values 0,1,2 and so on. The

25. What are the operators available in C++?  Arithmetic operator  Relational Operator  Assignment Operator  Logical Operator  Increment/ decrement Operator  Conditional operator  Bitwise operator  Special operator  Scope resolution operator  Pointer to member declaration  Pointer to member operator  Memory release operator  Line feed operator  Filed width operator 26.Explain about Type Casting Operator This is used for conversion of variables or expression explicitly Syntax: (type – name) expression or Type –name ( expression) Ex: avg= sum/ float(i); Alternatively, we can use typedef to create an identifier of the required type and use it in the functional notation typedef int * int_pt; p= int_pt(q); 27.What are the types of expression?  Constant expression  Integral expression  Float expression  Pointer Expression  Relational Expression  Logical expression  Bitwise expression 28. What is meant by operator overloading? Overloading means assigning different meaning to an operation, depending on the context. That is it is used to assign multiple meanings to operators.

Ex: The operator * when applied to a pointer variable, gives the value pointed to by the pointer. But it is used for multiplying two numbers.

29. What are the types of control strucuture  sequence  selection  iteration 30.What are the advantages of new operator  It automatically computes the size of the data object. So there is no need to use sizeof operator  It automatically returns the correct pointer type, so that here is no need to use a type cast.  It is possible to initialize the object while creating the memory space  Like any other operator, operator new and delete can be overloaded.

Unit-II

  1. What is a class? It is an extension to the structure data type. A class can have both variables and functions as members
  2. What is the difference between structure and a class? The only difference between a structure and a class in C++ is that , by default , the members of a class are private, while , by default the members of a structure are public.
  3. What is the specification for a class?  Class declaration  Class function definitions 
  4. What are data members and member functions? The variables declared inside the class are known as data members and the functions are known as member functions. The data members are usually private and member functions as public.
  1. When do we declare a member of a class static? When it is used to maintain values common to the entire class. The static member variables defined outside the class
  2. What is a friend function? The functions that are declared with the keyword friend are known as friend functions. A function can be declared as a friend in any number of classes, it has full access rights to the private members of the class.
  3. What are the special characteristics of friend function?  Can be invoked like a normal function, with the help of the object  It has the objects as arguments  It is not in the scope of the class to which is has been declared has friend
  4. What is const member function? If a member function does not alter any data in the class, then we declare it as const member function. The keyword const is appended to the function prototype.
  5. What is a constructor? It is a special member function whose task is to initialize the objects of its class. It is special because its name is the same name as the class name.
  6. How do we invoke constructor function? It is invoked whenever an object of an associated class is created. It is called constructor because it constructs the values of data members of the class.
  7. List some special properties of constructor functions.  They should be declared in the public section  They are invoked automatically when the objects are created  They do not have return types, therefore they cannot return values  They cannot be inherited  They can have default arguments  Cannot refer to addresses
  8. what is parameterized constructor? It is nothing but passing arguments to the constructor function when the objects are created. The constructor can take arguments are called parameterized constructor.
  9. What is copy constructor? The constructor that creates a new class object from an existing object of the same

class.

  1. What is dynamic initialization of objects? The initial value of an object provided at the run time. The advantage is that we can provide various initialization formats ,using overloaded constructors.
  2. What is dynamic constructor? Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of new operator.
  3. What is a destructor? It is used to destroy the objects that have been created by a constructor, when they no longer required.

Unit-III

1.What is meant by reusability Reusability is a feature which is supported in object-oriented programming. This allows the reuse of existing classes without redefinition. 2.Define Inheritance  In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined.  The former, known as derived classes , take over (or inherit ) attributes and behavior of the latter, which are referred to as base classes.  It is intended to help reuse of existing code with little or no modification.  Inheritance is also called generalization , because the is-a relationships capture a hierarchal relationship between classes of objects 3.What are the applications of Inheritance There are many different aspects to inheritance. Different uses focus on different properties, such as the external behavior of objects, internal structure of the object, structure of the inheritance hierarchy, or software engineering properties of inheritance. (i)Specialization One common reason to use inheritance is to create specializations of existing classes or objects. This is often called subtyping when applied to classes. In specialization, the new class or object has data or behavior aspects that are not part of the inherited class. Another form of specialization occurs when an inherited class specifies that it has a

it is called hierarchical inheritance.

6. How to define derived classes? A derived class can be defined by specifying its relational ship with the base class in addition to its own details. The syntax is: class derived-class-name : visibility-mode base-class-name { …… ……. }; Here the visibility mode is optional and if present, may be either private or public. The default mode is private. When a base class is privately inherited by a derived class, public members of the base class become private members of the derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class. When the base class is publicly inherited, public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class. 7. What is inherited from the base class? In principle, a derived class inherits every member of a base class except:  its constructor and its destructor  its operator=() members  its friends Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed. 8.Define virtual base class  A base class that has been qualified as virtual in the inheritance definition.  In multiple inheritance, a derived class can inherit the members of a base class via two or more inheritance paths.  If the base class is not virtual, the derived class will inherit more than one copy of the members of the base class. 9. Define virtual function  It is a function qualified by the virtual keyword. When a virtual function is called via a pointer, the class of the object pointed to determines which function definition will be used.  Virtual functions implement polymorphism, whereby objects belonging to different classes can respond to the same message in different ways.

10. What is meant by pure virtual function  A virtual function that is declared in a base class but not defined there. The responsibility for defining the function falls on the derived classes, each of which generally provides a different definitions.  It is illegal to create instances of a class that declares a pure virtual function. So such a class is necessarily an abstract base class. 11. What is meant by subclass and superclass Subclass: a class which has link to a more general class Superclass: a class which has one or more members which are classes themselves. 12. Define abstract class Abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class. It is a design concept in program development and provides a base upon which other classes may be built. 13.What are the forms of inheritance