studentsjungle@gmail.com

+91 7871727977

Students Jungle - Jobs, Events, News, Colleges, Schools
MENU
Home
Jobs
College Jobs School Jobs Government Jobs
Events
Conferences Symposiums
Colleges
Engineering Colleges Arts and Science Colleges
Posts Materials
Interview Questions
Programming General Marketing

C Interview Questions

What is difference between C and C++ ?
C++ is Multi-Paradigm ( not pure OOP, supports both procedural and object oriented) while C follows procedural style programming.
    In C data security is less, but in C++ you can use modifiers for your class members to make it inaccessible from outside.
    C follows top-down approach ( solution is created in step by step manner, like each step is processed into details as we proceed ) but C++ follows a bottom-up approach ( where base elements are established first and are linked to make complex solutions ).
    C++ supports function overloading while C does not support it.
    C++ allows use of functions in structures, but C does not permit that.
    C++ supports reference variables ( two variables can point to same memory location ). C does not support this.
    C does not have a built in exception handling framework, though we can emulate it with other mechanism. C++ directly supports exception handling, which makes life of developer easy.

What is a class?
Class defines a datatype, it's type definition of category of thing(s). But a class actually does not define the data, it just specifies the structure of data. To use them you need to create objects out of the class. Class can be considered as a blueprint of a building, you can not stay inside blueprint of building, you need to construct building(s) out of that plan. You can create any number of buildings from the blueprint, similarly you can create any number of objects from a class.
 class Vehicle
    {
        public:
            int numberOfTyres;
            double engineCapacity;
            void drive(){
                // code to drive the car
            }
    };

What is an Object/Instance?
Object is the instance of a class, which is concrete. From the above example, we can create instance of class Vehicle as given below
Vehicle vehicleObject;
We can have different objects of the class Vehicle, for example we can have Vehicle objects with 2 tyres, 4tyres etc. Similarly different engine capacities as well.

What do you mean by C++ access specifiers ?
Access specifiers are used to define how the members (functions and variables) can be accessed outside the class. There are three access specifiers defined which are public, private, and protected
 private:
    Members declared as private are accessible only with in the same class and they cannot be accessed outside the class they are declared.
    public:
    Members declared as public are accessible from any where.
    protected:
    Members declared as protected can not be accessed from outside the class except a child class. This access specifier has significance in the context of inheritance.

What are the basics concepts of OOP?
Encapsulation
Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from outside world. It's also called data hiding. In c++, encapsulation achieved using the access specifiers (private, public and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data. Consider the below class
class Person
    {
       private:
          int age;
       public:
          int getAge(){
            return age;
          }
          int setAge(int value){
            if(value > 0){
                age = value;
            }
          }
    };

In the class Person, access to the data field age is protected by declaring it as private and providing public access methods. What would have happened if there was no access methods and the field age was public? Anybody who has a Person object can set an invalid value (negative or very large value) for the age field. So by encapsulation we can preventing direct access from outside, and thus have complete control, protection and integrity of the data.
 Data abstraction
Data abstraction refers to hiding the internal implementations and show only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes.
 class Stack
    {
        public:
            virtual void push(int)=0;
            virtual int pop()=0;
    };
     
    class MyStack : public Stack
    {
        private:
            int arrayToHoldData[]; //Holds the data from stack
     
        public:
            void push(int) {
                // implement push operation using array
            }
            int pop(){
                // implement pop operation using array
            }
    In the above example, the outside world only need to know about the Stack class and its push, pop operations. Internally stack can be implemented using arrays or linked lists or queues or anything that you can think of. This means, as long as the push and pop method performs the operations work as expected, you have the freedom to change the internal implementation with out affecting other applications that use your Stack class.
 Inheritance
Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class.
 class SymmetricShape
    {
        public:
            int getSize()
            {
                return size;
            }
            void setSize(int w)
            {
                size = w;
            }
        protected:
            int size;
    };
     
    // Derived class
    class Square: public SymmetricShape
    {
        public:
            int getArea()
            {
                return (size * size);
            }
    };
In the above example, class Square inherits the properties and methods of class SymmetricShape. Inheritance is the one of the very important concepts in C++/OOP. It helps to modularise the code, improve reusability and reduces tight coupling between components of the system.

What is the use of volatile keyword in c++? Give an example.
Most of the times compilers will do optimization to the code to speed up the program. For example in the below code,
 int a = 10;
    while( a == 10){
         // Do something
    }
compiler may think that value of 'a' is not getting changed from the program and replace it with 'while(true)', which will result in an infinite loop. In actual scenario the value of 'a' may be getting updated from outside of the program.
Volatile keyword is used to tell compiler that the variable declared using volatile may be used from outside the current scope so that compiler wont apply any optimization. This matters only in case of multi-threaded applications.
In the above example if variable 'a' was declared using volatile, compiler will not optimize it. In shot, value of the volatile variables will be read from the memory location directly.

Explain abstraction.
- Simplified view of an object in user’s language is called abstraction.
- It is the simplest, well-defined interface to an object in OO and C++ that provides all the expected features and services to the user in a safe and predictable manner.
- It provides all the information that the user requires.
- Good domain knowledge is important for effective abstraction.
- It separates specifications from implementation & keeps the code simpler and more stable.

What is the real purpose of class – to export data?
No, the real purpose of a class is not to export data. Rather, it is to provide services. Class provides a way to abstract behaviour rather than just encapsulating the bits.
What things would you remember while making an interface?
 A class’s interface should be sensible enough. It should behave the way user expects it to.
It should be designed from the outside in.

Explain the benefits of proper inheritance.
The biggest benefits of proper inheritance are :
1. Substitutability    2. Extensibility.
Substitutability :
The objects of a properly derived class can be easily and safely substituted for an object of its base class.
Extensibility :
The properly derived class can be freely and safely used in place of its base class even if the properly derived class is created a lot later than defining the user code. Extending the functionalities of a system is much easier when you add a properly derived class containing enhanced functionalities.

Does improper inheritance have a potential to wreck a project?
Many projects meet a dead end because of bad inheritance. So, it certainly has the potential to wreck a project.

Small projects still have a scope to avoid the complete consequence of bad inheritance if the developers communicate and co-ordinate with an easy system design. This kind of a luxury is not possible in big projects, which means that the code breaks in a way difficult and at times impossible way to fix it.
How should runtime errors be handled in C++?
 The runtime errors in C++ can be handled using exceptions.
This exception handling mechanism in C++ is developed to handle the errors in software made up of independently developed components operating in one process and under synchronous control.
According to C++, any routine that does not fulfil its promise throws an exception. The caller who knows the way to handle these exceptions can catch it.

When should a function throw an exception?
A function should throw an exception when it is not able to fulfil its promise.
 As soon as the function detects a problem that prevents it from fulfilling its promise, it should throw an exception.
 If the function is able to handle the problem, recover itself and deliver the promise, then the exception should not be thrown.
 If an event happens very frequently then exception handling is not the best way to deal with it. It requires proper fixation.

Where are setjmp and longjmp used in C++?
Setjmp and longjmp should not be used in C++.
 Longjmp jumps out of the function without unwinding the stack. This means that the local objects generated are not destructed properly.
 The better option is to use try/catch/throw instead. They properly destruct the local objects.

Are there any special rules about inlining?
Yes, there are a few rules about inlining :
    Any source files that used the inline function must contain the function’s definition.
    An inline function must be defined everywhere. The easier way to deal with this to define the function once in the class header file and include the definition as required. The harder way is to redefine the function everywhere and learn the one-definition rule.
    Main() can not be inline.

Explain One-Definition Rule (ODR).
1. According to one-definition rule, C++ constructs must be identically defined in every compilation unit they are used in.
2.  As per ODR, two definitions contained in different source files are called to be identically defined if they token-for-token identical. The tokens should have same meaning in both source files.
3.  Identically defined doesn’t mean character-by-character equivalence. Two definitions can have different whitespace or comments and yet be identical.

What are the advantages of using friend classes?
1. Friend classes are useful when a class wants to hide features from users which are needed only by another, tightly coupled class.
2. Implementation details can be kept safe by providing friend status to a tightly cohesive class.

What is the use of default constructor?
 It is a constructor that does not accept any parameters.
If there is no user-defined constructor for a class, the compiler declares a default parameterless constructor called default constructor.
 It is an inline public member of its class.
 When the compiler uses this constructor to create an object – the constructor will have no constructor initializer and a null body.

Differentiate between class and structure.
The members of structures are public while those of a class are private.
 Classes provide data hiding while structures don’t.
 Class bind both data as well as member functions while structures contain only data

Explain container class.
Class to hold objects in memory or external storage. It acts as a generic holder.
It has a predefined behaviour and a known interface.
 It is used to hide the topology used for maintaining the list of objects in memory.
    The container class can be of two types:
1. Heterogeneous container : Here the container class contains a group of mixed objects
2. Homogeneous container : Here the container contains all the same objects.

What is namespace?
Namespaces are used to group entities like classes, objects and functions under a name.
Explain explicit container.
 These are constructors that cannot take part in an implicit conversion.
These are conversion constructors declared with explicit keyword.
Explicit container is reserved explicitly for construction. It is not used by the compiler to implement an implied conversion of types.

Explain class invariant.
It is a condition that ensures correct working of a class and defines all the valid states for an object.
 When an object is created class invariants must hold.
 It is necessary for them to be preserved under all operations of the class.
All class invariants are both preconditions as well as post-conditions for all operations or member functions of the class.

Differentiate between late binding and early binding. What are the advantages of early binding?
 Late binding refers to function calls that are not resolved until run time while early binding refers to the events that occur at compile time.
 Late binding occurs through virtual functions while early binding takes place when all the information needed to call a function is known at the time of compiling.
Early binding increases the efficiency. Some of the examples of early binding are normal function calls, overloaded function calls, and overloaded operators etc.

Explain public, protected, private in C++?
These are three access specifiers in C++.
1. Public : Here the data members and functions are accessible outside the class.
2. Protected : Data members and functions are available to derived classes only.
3. Private : Data members and functions are not accessible outside the class.

123
New Mobile App for Jobs in Colleges

New Mobile App for Jobs in Schools

Latest News

SSLC(10th) Study Material

HSC(12th) Study Material

Tags Cloud

Jobs Jobs in Colleges Jobs in Schools Government Jobs Conferences Symposiums International Confrences Materials Educational News Interview Questions
Terms of use Privacy policy Advertise with us About Us Contact us

Copyright © SJ :: Students Jungle. All Rights Reserved

Developed By SMS :: SOFT MaX SOLUTIONS