C/C++ Interview Questions Review

Ace your homework & exams now with Quizwiz!

What is operator overloading?

Operator overloading is a mechanism in which a special meaning is given to an operator. For example, you can overload the '+' operator in a class-like string to concatenate two strings by only using '+.'

Which among the following operators cannot be overloaded? -, +, ?:, %

?:

What is the C++ OOPs concept?

Object: Anything that exists physically in the real world is called an object. Class: The collection of objects is called class. Inheritance: Properties of parent class inherited into child class is known as inheritance. Polymorphism: It is the ability to exist in more than one form. Encapsulation: Binding of code and data together into a single unit. Abstraction: Hiding internal details and showing functionality to the user.

Access Modifiers

Private Public Protected

Difference between prefix and postfix incrementing operators.

++i vs i++

Difference between equal to (==) and assignment operator(=)?

== operator - returns a boolean value. If the expression on the right returns the same value as the expression on the left then it will return true, otherwise false. = operator - assignment operator, assigns the value on the right hand side expression to the left operand.

What is the size of the int data type?

32 bits or 4 bytes

What is the output of the below C++ program?

4810

What are the four different data types in C++?

- Primitive/Basic: Char, int, short, float, double, long, bool, etc. - Derived: Array, pointer, etc. - Enumeration: Enum - User-defined: Structure, class, etc.

What is the output of the below C++ program?

0 6

What is the output on the program in the image?

012

What is an abstract class? When is it used?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

Define operator overloading and function overloading.

An example of compile-time polymorphism is operator overloading. It is the concept of modifying an existing C++ operator without altering its original meaning. int operator+( int lhs, const myclass& rhs ) { return lhs + (int)rhs; } log(std::string val); log(int val);

What among these is used to return the number of characters in the string?

Both size and length are used to return the number of characters in the string

Call by reference vs Call by pointer

Calling by reference abstracts the fact that a variable is a pointer. When called by pointer you can change the address of a pointer. When doing it by reference the function that is called can not alter the pointer address.

What are classes and objects in C++?

Classes are like blueprints for objects. Classes have data members and methods. Objects of a class type contain persistent data that belong to that specific object.

Compare compile time and runtime polymorphism.

Compile-time Polymorphism - The method to be executed is known at compile time. And the call is resolved by the compiler. - Provides quicker execution because it is known at the compile time. - Achieved by operation or function overloading. Runtime Polymorphism - The method to be executed is known at run time. The compiler does not resolve the call. - Provides slower execution because it is known at the run time. - Achieved by function overriding.

Which of the following is not a member of a class? Static function Virtual function Const function Friend function

Friend function

What are void pointers?

In C, a void pointer has no connection to any particular data type. It designates a location for specific data within the storage. This indicates that it is pointing to a variable's address. It also goes by the name "general purpose pointer."

What is call by value and call by reference in C++?

In the call by value method, you pass the copies of actual parameters to the function's formal parameters. This means if there is any change in the values inside the function, then that change will not affect the actual values. In the call-by-reference method, the reference or address of actual parameters is sent to the function's formal parameters. This means any change in values inside the function will be reflected in the actual values.

What is inheritance?

Inheritance is the mechanism in which you can create a new class i.e. child class from the existing class i.e. parent class. This child class is also known as a derived class and the parent class is also called Base class.

What are the three different types of C++ access specifiers?

Public: All member functions and data members are accessible outside the class. Protected: All member functions and data members are accessible within the class and to the derived class. Private: All member functions and data members cannot be accessed outside the class.

What do you understand about polymorphism in C++?

The term polymorphism refers to the presence of multiple forms. Polymorphism usually occurs when there is a hierarchy of classes that are linked by inheritance. C++ polymorphism means that depending on the type of object that invokes the function, a different function will be executed.

What is the difference between a while loop and a do-while loop?

While checks before any iteration, do while always iterates at least once. Some while loops may be turned into do while loops in assembly by a compiler.

Can you compile a program without the main function?

Yes, but the compiler may try to prevent you from doing this. However main is the entry point into a program from the kernel.

Ternary operator

condition ? expression1 : expression2; if condition is true, expression1 is executed. And, if condition is false, expression2 is executed.

Discuss the difference between new and malloc

new : new is an operator It calls the constructor There is no need to specify memory size while using new() new operator can be overloaded malloc : malloc() is a function The malloc function doesn't call the constructor You have to specify the memory size malloc() can never be overloaded

How to input strings in C++ with spaces?

or use std::cin

What is used to return the number of characters in the string?

size or length methods

Which of the following will give the size of object or type?

sizeof operator : sizeof expression sizeof(type)

What is std in C++?

std = standard namespace

What should be the output of the below code?

1010, the value of j is incremented to 11, then j value is added to 100 but not assigned, and at last the value of j i.e 11 is added to 999 which gives us 1010.

What should be the output of the below code?

6

What is the output of the below C++ program?

7

What is this pointer in C++?

A class, struct, or union form only has access to this pointer within non-static member variables. The arrow shows the object for which the member function is called. string food = "Pizza"; // A food variable of type string string* ptr = &food; // A pointer variable, with the name ptr, that stores the address of food // Output the value of food (Pizza) cout << food << "\n"; // Output the memory address of food (0x6dfed4) cout << &food << "\n"; // Output the memory address of food with the pointer (0x6dfed4) cout << ptr << "\n";

What is a constructor?

A constructor is defined as a member function that is invoked whenever you create an object; it has the same name as that of the class. There are two types of constructors: Default constructor: This auto-generated constructor doesn't take any arguments. Parameterized constructor: In this constructor, it can pass arguments.

What is a copy constructor?

A copy function is a member function that uses another object from the same class to initialize a new thing. A copy function is, to put it simply, a function that produces an object by initializing it with a different object of the same class that has already been constructed.

What is a constructor in C++?

A member function of a class that has the same name as the class itself. It is called when an object is created, initializes an objects data members.

What is a scope resolution operator?

A scope resolution operator is represented as :: This operator is used to associate function definition to a particular class. The scope operator is used for the following purposes: To access a global variable when you have a local variable with the same name. To define a function outside the class.

How are virtual functions different from pure virtual functions?

A virtual function is a base class member function that a derived class can modify. A member function of a base class that is a pure virtual function must be defined in the derived type; otherwise, the derived class will become abstract as well.

What is a virtual function?

A virtual function is a member function which is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Can a virtual function be called from a constructor?

A virtual process may be called a function Object, but exercise caution. It might perform differently than expected. The virtual call mechanism in a function Object is disabled since overriding from derived classes hasn't happened yet. Building blocks are used to create objects, "base before derived."

What is Abstraction?

Abstraction can be defined as a technique in which you only show functionality to the user i.e., the details that you want the user to see, hiding the internal details or implementation details.

What is an abstraction in C++?

Abstraction means displaying the essential details to the user while hiding the irrelevant or particular details that you don't want to show to a user. It is of two types: Control abstraction Data abstraction

What is the difference between C and C++?

C : - C is a procedure-oriented programming language - C doesn't support function or operator overloading - C language doesn't support virtual and friend function - C language has 32 keywords C++ : - C++ is a partially object-oriented programming language - C++ supports function as well as function overloading - C++ language supports both virtual and friend functions. - C++ language contains 52 keywords

What do you understand about static members and static member functions?

Data for static members are within the data segment of memory. They are defined at compile time A variable in a class declared as static has its space allocated for the lifetime of the program. Regardless of the number of objects of that class created, there is only a single copy of the static member. The same static member is accessible to all the objects of that class. A static member function can be called even if no class objects exist. It is accessed using only the class name and the scope resolution operator (::).

What is an inline function?

Inline tells the compiler to implement the function body into the place where the function is called. This improves speed at runtime as a regular function call would require passing arguments and adding another namespace to the stack for the local environment of that function call. Inline functions basically remove the function call system. inline keyword goes where the function definition is

What do you understand about friend class and friend function?

It allows a function that is not defined within a class to access the private data members of an object. (PPM operator example)

Is it possible to overload a deconstructor? Give reasons for your answer.

It is not possible to 'override' a deconstructor as it takes no arguments. One caveat is that you can declare a different deconstructor on a derived class.

Class D is derived from a base class B. If creating an object of type D, what order will the constructors of these classes get called?

Parent first The derived class consists of two parts: the base part and the derived part. C++ constructs derived objects in phases. The process begins with constructing the most-base class (at the top of the inheritance tree), followed by each child class construction in order, and then the most-child class. Thus, first, the Constructor of class B will be called, and then the constructor of class D.

What are pointers in C++?

Pointers are the variables that store the memory address of another variable. The type of the variable must correspond with the type of pointer. type *name

What are destructors in C++?

Same as a constructor but called when an object is destroyed either by the delete keyword if on the heap, and when removed from the stack as an object falls out of scope. Defined as by a ~ (tilda) followed by the name of the class, takes no parameters.

How is a shallow copy different from a deep copy?

Shallow Copy - It stores the references of objects to the original memory address. - Faster - It reflects changes made to the new/copied object in the original object. Deep Copy - It makes a fresh and separate copy of an entire object and its unique memory address. - Comparatively slower - It doesn't reflect changes made to the new/copied object in the original object. C++ map copy

What should be the correct statement about string objects in C++? String objects should necessarily be terminated by a null character String objects have a static size String objects have a dynamic size String objects use extra memory than required

String objects have a dynamic size.

How is struct different from class?

Struct: - Members are public by default - Default access specifiers are public when deriving a struct from a class/struct. Class: - Members are private by default - Default access specifiers are private when deriving a class.

How would you deallocate and allocate memory in C++?

The heap is used in C to allocate dynamic memory, and these functions are part of the standard library. Malloc() and free are the two important dynamic memory operations (). The size of the desired memory area in bytes is the only parameter accepted by the malloc() function.

What is a friend function?

You can define a friend function as a function that can access private, public and protect members of the class. You declare the friend function with the help of the friend keyword. You declare this function inside the class.

When is void() return type used?

You use the void() return type when you don't want to return any value. It specifies that the function doesn't return a value. A function with a void return type completes its task and then returns the control to the caller.

What is STL?

a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc Think vector, multimap, map, unordered map.


Related study sets

Audit Sampling (Chapter 9 Auditing)

View Set

Ch. 12 Vocab (Nutritional Assessment)

View Set

Combo with "Mental Health week 1" and 10 others

View Set

dance is an activity which can take many forms and different needs .it can be recreation, entertainment education, therapy and religion.pep

View Set

Osteoporosis NCLEX Style Questions

View Set

6.3 - Triumph of Parliament in England

View Set