0% found this document useful (0 votes)
31 views22 pages

Important Question

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views22 pages

Important Question

Uploaded by

Ameer Hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Object Orientated Programming Concepts

Question #1 what is the difference between dynamic and static polymorphism?

Answer: Static polymorphism is done by method overloading and Dynamic polymorphism is done by
method overriding.

Q I have a method in the Base class. I have overridden that method in the
derived class with some new implementation. Now I make the pointer of
Base class and store the address of base class in it. Now I call the
overridden method. Now which method is called, the one that is defined in
the base class or the one that is defined in the derived class.
ANS: It can be broken down in to two cases:

Non-virtual methods:
If the method in the base class is non-virtual, then calling it through a
pointer to the base class will always invoke the base class implementation,
regardless of whether the derived class has overridden it.

Virtual methods:
If the method in the base class is virtual, then calling it through a pointer
to the base class will dynamically dispatch to the derived class
implementation (if it exists).

Question #2 Difference between private and protected access modifiers?

Answer: Members declared as private are accessible only within the same class in which they are
declared. They are not accessible from outside the class, including subclasses (inheritance), or other
classes.
Members declared as protected are accessible within the same class, its subclasses (inheritance).This
access level is useful when you want to make certain members accessible to subclasses but not to the
entire program.

Question#3 What is Is-A relationship?

Answer: Parent child relation is also named as Is-A relation.

Example: Honda is a Car.

Question#4 What is virtual function?

Answer: Overriding is done by using virtual keyword with the function. A function that is overridden in
the derived class is a virtual function.

Question#5 What is difference between “virtual” and “pure Virtual” Functions?

Virtual Function Pure Virtual Function

Base class must provides the implementation of Base class must NOT provides the implementation
virtual function as a default case. of virtual function as a default case.

Derived class may or may NOT implement virtual Derived class must implement virtual fucntion
fucntion

It results in run-time polymorphism It provides like an intreface which the drived class
must have to implement.

A class that has a pure virtual function may NOT be


instantiated

Question# 6 Does C++ support multiple and multilevel inheritance?

Answer: Yes, C++ support both multiple and multilevel inheritance.

Question#7 What is an abstract class ?

Answer: A class that must have atleast one pure virtual function. Abstract class is like a half defined or
partial class. We have to redefine the virtual methods of the abstract class in the derived class. The
purpose of the abstract class is to provide a contract that all the classes have to fill if they inherit the
contract.This class cannot be instantiated. But we can create pointers of the abstarct class. Here is the
example of how to do this:
#include <iostream>

// Abstract class

class Shape {

public:

// Pure virtual function (abstract method)

virtual double area() const = 0;

// Concrete method

void display() const {

std::cout << "This is a shape." << std::endl;

};

// Concrete subclass

class Rectangle : public Shape {

private:

double length;

double width;

public:

Rectangle(double length, double width) : length(length), width(width) {}

// Implementation of the pure virtual function

double area() const override {

return length * width;

};
int main() {

// Shape shape; // Error: Cannot instantiate abstract class

Rectangle rectangle(5, 3);

rectangle.display(); // Output: This is a shape.

std::cout << "Area: " << rectangle.area() << std::endl; // Output: Area: 15

return 0;

Question#8 What is use of creating pointers of abstarct class. Why it is allowed to create pointers of
abstract class?

ANS: Pointers to abstract classes allow for polymorphic behavior, where a single pointer can refer to
objects of different derived classes. This enables you to write code that operates on a base class
interface without needing to know the specific derived class implementation at compile time. Here is an
example of how to do polymorphism using pointer of abstract class:

#include <iostream>

// Abstract base class

class Shape {

public:

// Pure virtual function

virtual void draw() const = 0;

// Virtual destructor (good practice for polymorphic behavior)

virtual ~Shape() {}

};

// Derived class Rectangle


class Rectangle : public Shape {

public:

// Override the pure virtual function

void draw() const override {

std::cout << "Drawing Rectangle" << std::endl;

};

// Derived class Circle

class Circle : public Shape {

public:

// Override the pure virtual function

void draw() const override {

std::cout << "Drawing Circle" << std::endl;

};

int main() {

Shape* shape1 = new Rectangle();

Shape* shape2 = new Circle();

// Polymorphism in action

shape1->draw(); // Output: Drawing Rectangle

shape2->draw(); // Output: Drawing Circle

delete shape1;

delete shape2;

return 0;
}

Question# 9 A method is defined as abstract in an abstract class. Does it is a virtual method?

Answer: Yes. Abstract methods inside the abstract class are by default virtual.

Question#10 What is an interface of a class?

Answer: Interface is like a contract between the base class and a child class. In C++, interfaces
methods are pure virtual function. We cannot create an instance of a interface class. The class that
defines intreface is called “Abstract Class”.

Question#11 Can methods of an interface be private?

Answer: By default all the methods of an interface are public. We cannot define a private method
inside an interface.

Question#12 Can a class inherit multiple interfaces?

Answer: Yes, an interface can have multiple inheritance. Means a class can inherit multiple interfaces
at the same time. When we want to change the interface, we do multiple inheritance.

Question#13 What is interface segregation?

Answer: Create different interfaces for different requirements of the user. Do not try to use same
interface with some added functions for a different requirement of a user.

Question#14 Can we inherit multiple abstract classes in a siagle class?

Answer: Yes, we can inherit multiple abstract classes in a single class.

Question#15 Difference Between the interface and abstract class?


Answer:

InterFace Abstract Class

Contains only methods NOT there implementation It is partial implemented class with atleast one
method that has to implemented in the derived
class called pure virtual function

Cannot has its own variables Can have its own variables

Cannot has implemented Function Must have atleast one non implemented function

Question#16 What is the significance of using public keyword with class when it is inherited in the
drived class. Why like this:
class C : public A, public B

Answer: These access specifiers control the accessibility of the inherited members within the derived
class and its derived classes.

->PUBLIC: Public inheritance means that the public members of the base class remain public in the
derived class, the protected members of the base class remain protected in the derived class, and the
private members of the base class remain inaccessible in the derived class. This is the most common
form of inheritance and is used to model an "is-a" relationship.

->PROTECTED: Protected inheritance means that the public and protected members of the base class
become protected in the derived class, and the private members of the base class remain inaccessible in
the derived class.

-> PRIVATE: Private inheritance means that the public and protected members of the base class
become private in the derived class, and the private members of the base class remain inaccessible in
the derived class. This form of inheritance is used to implement composition or to model a "has-a"
relationship.
Question#17 What are pure virtual functions?

Answer: Pure virtual functions are defined as: public virtual function=0; . Means they are not
defined inside the abstract class. Interface can have pure virtual function.

Question#18 Difference between IS-A and HAS-A relationship?

Answer:

IS-A relationship HAS-A relationship

IS-A relationship is achieved using inheritance in A relationship is said to be a HAS-A relationship,


programming when we use instance of a class inside the other
class.

Reactangle IS-A shape For exmaple CAR class HS-A ENGINE class inside it

Weak than HAS- relationship Strong relation than IS-A relation

3 Types:

1. Assosiation 2.Aggregation

2. Composition

Example HAS-A relation

class Engine{

int number;

int strokes;

class car{

Engine e;

void print(){

cout<<”Car HAS-A engine”}

Example IS-A relation:


class shape{

double length;

double width

class rectangle:public shape{

public:

void print(){

cout<<”Rectangle IS-A shape”

Question#19 Difference between Composition and Aggregation?

Answer:

Composition Aggregation

In composition, derived class cannot exist In aggregation, a derived can exist without the
independently. existence of the base class. It is like a weak
relationship.

.It is a strong HAS-A relation. It is like weak HAS-A relation.

Achieved by using object of one class inside the Achieved BY pointers


other class

// Example of Composition

class Engine {

public:

void start() { cout << "Engine started." << endl; }

};
class Car {

private:

Engine engine; //A car is composed of a engine

public:

void start() { engine.start(); }

};

// Example of Aggregation

class Wheel {

public:

void rotate() { cout << "Wheel rotating." << endl; }

};

class Car2 {

private:

Wheel* wheels[4]; //A car can exist without wheels. It is an aggregation relationship. So pointers are
used

public:

Car2(Wheel* w1, Wheel* w2, Wheel* w3, Wheel* w4) {

wheels[0] = w1;

wheels[1] = w2;

wheels[2] = w3;

wheels[3] = w4;

void drive() {

for (int i = 0; i < 4; i++) {

wheels[i]->rotate();

}
}

};

Question#20 What is an association relationship?

Answer: Association is a relationship between two classes. Any dependency relationship between
two class is an association relation. Aggregation and composition both are subset of association
relation.

Example: Problem and patient has composition relationship. No patient, no problem.

Patient and Doctor has aggregation relationship. A doctor can exist without a patient

Patient and bed has association relationship.

Question# 21 UML diagrams for association, aggregation and composition.

Answer:

NOTE:

-> Class A has an association with class B.


-> ClassB is aggregated of ClassA. Means classB contains pointer of class A.

-> Class B is composed of classA. Means class B contains object of class A.

Question#22 What is multiple inheritence ? Does it creates any problem?

Answer: When one class inherit from two different classes at the same time, it is called multiple
inheritance. It leands to the diamond shape problem.

class A{

void print(){

cout<<”Thi is class A”

class B{

void print(){

cout<<”Thi is class B”

class C:public classA, publicB{

void print(){

cout<<”This is class A”

Question#23 What is diamond shape problem? How C++ and Python resove this problem?

Question#24 What is virtual inheritance?

Question What are friend fucntions and friend classes. Does a class has more than one friend
functions?

Answer: Yes, a class may have more than one functions. Friend functions allow you to access the private
members of a class.Here how to do this:

Friend Function:
So, “showValue” fucntion is the friend fucntion of classB

Friend Class:

So, class B is the friend of class A.

IMPORTANT:

-> A class can have more than one friends

-> Friendship Not Inherited: Friendship is not inherited. If Class B is a friend of Class A, and Class C
derives from Class A, then Class C does not automatically have access to the private or protected
members of Class B.

-> Friend fucntion must be a global function


Question#25 Difference between overridding and overloading?

Overridding Overloading

Method overriding happens when a subclass Overloading happens when two functions having
provides a specific implementation of a method the same return type but have different function
that is already defined in its superclass. signatures(function name, number of arguments,
type of arguments)

Method overriding is possible because of No role of inheritence in method overloading


inheritance and abstraction. Subclasses inherit
methods from their superclasses.

The overriding method must have the same Overloading methods must have different function
method signature as the method it is overriding. signatures. This includes name, number of
This includes the method name, parameter types, parameters, and parameter types. It does not
and return type. includes functions return type and name of
parameters

It is resolved at run-time It is resolved at compile-time

Here is the example of how to do overridding:


Question#26 What is the use of super keyword?

Answer: It is used to call the parent method or variable in a child class. We can also creates an object of
child class and than calls the method or varaible of parent class.
Question#27

Structure Query Language (SQL)

Question#1 What is denormalization? Why we need denormalization?


Answer: Joining the tables to improve the efficiency of the query. Lesser the tables, more
efficient the query is. Denormilization can produce redundant data.
Question#2 What is Online Transection Processing(OLTP) and Online Analytical Processing
(OLAP) ?

Answer: For high intense system, there are two databases. One is OLTP and other is OLAP.
All the transection processes like(Insert, update, delete) they are done using OLTP. In OLTP, we
normalize the tables to achieve more efficiency. Transection queries are done with high
efficiency. Reading, Selecting and other analytical operations are done on OLAP databases.
Because in OLAP, we do not need high efficiency. We need quick results. So we do
denormalization in OLAP databases.

Question#3 When the table is in 1st Normal Form?


Answer: When columns have atomic values. It should not have repeating groups. Every
column has only one value. Solution is to split the table

Question#4 When the table is in 2nd Normal Form?


Answer: It must be in 1st normal form. All non-primary attributes depends completely on the
prime attributes(join prime attributes). No partial dependency. Solution is to split the table in to
two.

Question#5 When the table is in 3rd Normal Form?


Answer: Table must be in 1st and 2nd normal form. There must be no transient dependency
in the table. A non-key attribute does not depend on other non-key attribute. All non-key
attributes should completely depend upon the primary attribute. Split the table.

We can remember the normalizations using word APT .A means Atomic, P means partial
and T means transient dependencies.

Question#6 What happens if the table is not in normal form?


Answer: Anomilize occur during the database query. Type of Anomilize are:

Question#7 Difference between Primary Key and Unique Key?


Answer: Unique key can have null values. There may be multiple unique keys in a table in the
database.

Question#8 Difference between char and varchar?

Answer: Char is of fixed length. While Varchar is of variable length. In varchar, the length can
be dynamically vary depending in usage.

Question#9 Difference between char and nchar?


Answer: char can only store English characters. NChar can store numbers, alphabets etc.

Question#10 How indexing help in effiency of the query?

Answer: When we apply indexing, it will create a balance tree. We can compare at any node
weather to go to left or right subtree using comparision operator. I this way half subtree is not
scanned. Index has two type clustered and non-clustered index.

Question#11 Difference between Clustered and non-clustered index?


Answer: Every index, clustered or non-clustered, it has a balance tree. In clustered index, the
leave node points to actual record or data. While in non-clustered index, the leave node takes
help of clustered index to locate the record.
All nodes that are present on level 1 are clustered index. Nodes on level>1 are non-clustered
index.
We have only one clustered index in table but we can have many non-clustered indexes.

Question# 12 Difference between Functions and Stored Procedures ?


Answer:

Functions Stored Procedures


Main goal is to get computed values It is a min brach program
Cannot make permanent changes to Make permanent changes to database
database
Only select is allowed. Do not allow to use Can use both select, update, insert, delete
insert, update and delete
Can be called from Stored Procedures Cannot be called from Functions

Question#13 Write code for a trigger?

Answer: CREATE TRIGGER log_changes

AFTER INSERT, UPDATE, DELETE ON my_table

FOR EACH ROW

BEGIN

INSERT INTO log_table (table_name, operation, timestamp)

VALUES ('my_table', IF(NEW.id IS NULL, 'DELETE', IF(OLD.id IS NULL, 'INSERT', 'UPDATE')), NOW());

END;

Question#14 What are different types of triggers?

Answer: After trigger and instead of trigger. After trigger is called after an event is successfully
happened. In Instead of triggers, the operations are not performed and a trigger is fired. Means trigger is
fired instead of operation. Operation is not performed.

Question#15 What is identity property of a column?

Answer: Identity is true, than values of a column got auto incremented. We cannot put values by
yourself in that column whose identity property is set to true.

Question# 16 What is a transaction?

Answer: It enable us to perform series of activities as one logical unit. If anyone operations is not
performed then complete transection is roll backed.

Question# 17 Write code for basic transection


Answer: begin try

begin tran

Insert into tbl1(FirstName) values(“hamza”)

Insert into tbl1(FirstName) values(“safeer”)

commit tran

end try

begin catch

rollback

Question# 18 What is an inner join?

Answer: Inner join selects matching records from both the tables. It works when both tables have some
matching columns. Example:

Select FirstName From EmpTable

Inner join Dept_table

On EmpTable.dept_id= Dept_table.dept_id.

Question#18 What is an left join?

Answer: In left join all values from left table are selected + but only matching values from right table.

Example:

SELECT Dept_id FROM

Dept_tbl LEFT JOIN emp_tabl

On Dept_tbl.id=emp_tbl.id.

Question#19 What is full outer join?

Answer: All matching and non-matching records from both left and right tables are selected.

Example:

SELECT FirstName FROM EmpTable

FULL OUTER JOIN Dept_table

On EmpTable.dept_id= Dept_table.dept_id.
Question#20 What is cross join?

Answer: It is like a cross join. Remember no on keyword in cross join. Example:

SELECT FirstName FROM EmpTable

CROSS JOIN Dept_table

EmpTable.dept_id= Dept_table.dept_id.

Question#21 Difference between full outer join and cross join?

Answer: In full outer join simply all the record in both tables are displayed. In cross join, every
record of table is matched with every record in the other table. Cross join is like a cartesion product of
two tables.

Question#22 Write query to Select nth highest value from a table?

Answer: We use DTAO (Don’t Tell Any One)

SELECT TOP 1 * FROM (SELECT TOP [Nth] salary FROM emp_tbl ORDER BY salary DESC)

AS INNERQUERY

ORDER BY salary ASC;


Question#23 How to make this query parametrized?

Answer: We can use co-related query because we cannot use variable with select.

Question#24 Which query is faster Co-related Query or Order By/Top ?

Answer:

You might also like