Skip to main content

C and C++ Interview Questions And Answers | C Interview Questions

Hello friends welcome to My blog "Interview Introduction ".  Today we are going to learn Interview questions with answers of C/C++ programming.
c programming interview questions and answers, c programming

C and C++ Programming Interview Questions And Answers

1)What is the full form of OOPS?

Ans: Object Oriented Programming System.

2)What is a class?

Ans: Class is a blue print which reflects the entities attributes and actions. Technically defining a class is designing an user defined data type.

3)What is an object?

Ans: An instance of the class is called as object.

4)List the types of inheritance supported in C++?

Ans: Single, Multilevel, Multiple, Hierarchical and Hybrid.

5)What is the role of protected access specifier?

Ans: If a class member is protected then it is accessible in the inherited class. However, outside the both the private and protected members are not accessible.

6)What is encapsulation?

Ans: The process of binding the data and the functions acting on the data together in an entity (class) called as encapsulation.

7)What is abstraction?

Ans: Abstraction refers to hiding the internal implementation and exhibiting only the necessary details.

8)What is inheritance?

Ans: Inheritance is the process of acquiring the properties of the exiting class into the new class. The existing class is called as base/parent class and the inherited class is called as derived/child class.

9)Explain the purpose of the keyword volatile?

Ans: Declaring a variable volatile directs the compiler that the variable can be changed externally. Hence avoiding compiler optimization on the variable reference.

10)What is an inline function?

Ans: A function prefixed with the keyword inline before the function definition is called as inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros.
C programming interview questions, inheritance

11)What is a storage class?

Ans: Storage class specifies the life or scope of symbols such as variable or functions.

12)Mention the storage classes names in C++?

Ans: The following are storage classes supported in C++ : auto, static, extern, register and mutable.

13)What is the role of mutable storage class specifier?

Ans: A constant class objects member variable can be altered by declaring it using mutable storage class specifier. Applicable only for non-static and non-constant member variable of the class.

14)Distinguish between shallow copy and deep copy?

Ans: Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator.

15)What is a pure virtual function?

Ans: A virtual function with no function body and assigned with a value zero is called as pure virtual function.

16)What is an abstract class in C++?

Ans: A class with at least one pure virtual function is called as abstract class. We cannot instantiate an abstract class.

17) What is a reference variable in C++? 

Ans: A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.

18)What is role of static keyword on class member variable?

Ans: A static variable does exit though the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.

19)Explain the static member function?

Ans: A static member function can be invoked using the class name as it exits before class objects comes into existence. It can access only static members of the class.

20)Name the data type which can be used to store wide characters in C++?

Ans: wchar_t

21)What are/is the operator/operators used to access the class members?

Ans: Dot (.) and Arrow ( -> )

22)Can we initialize a class/structure member variable as soon as the same is defined?

Ans: No, Defining a class/structure is just a type definition and will not allocated memory for the same.

23)What is the data type to store the Boolean value?

Ans: bool, is the new primitive data type introduced in C++ language.

24)What is function overloading?

Ans: Defining several functions with the same name with unique list of parameters is called as function overloading.

25)What is operator overloading?

Ans: Defining a new job for the existing operator w.r.t the class objects is called as operator overloading.

26)Do we have a String primitive data type in C++?

Ans: No, its a class from STL (Standard template library).

27)Name the default standard streams in C++?

Ans: cin, cout, cerr and clog.

28)Which access specifier/s can help to achive data hiding in C++?

Ans: Private and Protected.

29)When a class member is defined outside the class, which operator can be used to associate the function definition to a particular class?

Ans: Scope resolution operator (::)

30)What is a destructor? Can it be overloaded?

Ans: A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.

31)What is a constructor?

Ans: A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.
What is a constructor in C

32)What is a default constructor? Can we provide one for our class?

Ans: Every class does have a constructor provided by the compiler if the programmer doesn’t provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler doesn't provides the constructor.

33)Which operator can be used in C++ to allocate dynamic memory?

Ans: new is the operator can be used for the same.

34)What is the purpose of ‘delete’ operator?

Ans: delete operator is used to release the dynamic memory which was created using new operator.

35)Can I use malloc() function of C language to allocate dynamic memory in C++?

Ans: Yes, as C is the subset of C++, we can all the functions of C in C++ too.

36)Can I use delete operator to release the memory which was allocated using malloc() function of C language?

Ans: No, we need to use free() of C language for the same.

37)What is a friend function?

Ans: A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword friend.

38)What is a copy constructor?

Ans: A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.

39)Does C++ supports exception handling? If so what are the keywords involved in achieving the same?

Ans: C++ does supports exception handling. try, catch and throw are keyword used for the same.

40)Explain the pointer – this?

Ans: This, is the pointer variable of the compiler which always holds the current active objects address.

41)What is the difference between the keywords struct and class in C++?

Ans: By default the members of struct are public and by default the members of the class are private.

42)Can we implement all the concepts of OOPS using the keyword struct?

Ans: Yes.

43)What is the block scope variable in C++?

Ans: A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block.

44)What is the role of the file opening mode ios::trunk?

Ans: If the file already exists, its content will be truncated before opening the file.

45)What is the scope resolution operator?

Ans: The scope resolution operator is used to: Resolve the scope of global variables and To associate function definition to a class if the function is defined outside the class.

46)What is a namespace?

Ans: A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under different name space.

47)What are command line arguments?

Ans: The arguments/parameters which are sent to the main() function while executing from the command line/console are called so. All the arguments sent are the strings only.
What are command line arguments in C

48)What is a class template?

Ans: A template class is a generic class. The keyword template can be used to define a class template.

49)How can we catch all kind of exceptions in a single catch block?

Ans: The catch block with ellipses.

50)What is keyword auto for? 

Ans: By default every local variable of the function is automatic (auto).

51)What is a static variable?

Ans: A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice. If a global variable is static then its visibility is limited to the same source code.



53)What is the purpose of extern storage specifier?

Ans: Used to resolve the scope of global symbol.

54)When should we use the register storage specifier?

Ans: If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.

55)Can a program be compiled without main() function?

Ans: Yes, it can be but cannot be executed, as the execution requires main() function definition.

56)Where an automatic variable is stored?

Ans: Every local variable by default being an auto variable is stored in stack memory.

57)What is a container class?

Ans: A class containing at least one member variable of another class type in it is called so.

58)What is a token?

Ans: A C++ program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
What is a token in C

59)What is a preprocessor?

Ans: Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

60)What are command line arguments?

Ans: The arguments which we pass to the main() function while executing the program are called as command line arguments.

61)What are the different ways of passing parameters to the functions? Which to use when?

Ans: Call by value: We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
Call by address: We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
Call by reference: The actual parameters are received with the C++ new reference variables as formal parameters. We choose this if we do want the actual parameters to be modified with formal parameters.

62)What is reminder for 5.0 % 2?

Ans: Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

63)Which compiler switch to be used for compiling the programs using math library with g++ compiler?

Ans: Opiton –lm to be used as > g++ –lm

64)Can we resize the allocated memory which was allocated using ‘new’ operator?

Ans: No, there is no such provision available.

65)Who designed C++ programming language?

Ans: Bjarne Stroustrup.

66)Which operator can be used to determine the size of a data type/class or variable/object?

Ans: sizeof

67)How can we refer to the global variable if the local and the global variable names are same?

Ans: We can apply scope resolution operator (::) to the for the scope of global variable.

68)What are valid operations on pointers?

Ans: he only two permitted operations on pointers are: Comparision ii) Addition/Substraction (excluding void pointers).

69)What is recursion?

Ans: Function calling itself is called as recursion.

70)What is the first string in the argument vector w.r.t command line arguments?

Ans: Program name.

71)What is the maximum length of an identifier?

Ans: Ideally it is 32 characters and also implementation dependent.

72)What is the default function call method?

Ans: y default the functions are called by value.

73)What are available mode of inheritance to inherit one class from another?

Ans: Public, private and protected

74)What is the difference between delete and delete[]?

Ans: Delete[] is used to release the array allocated memory which was allocated using new[] and delete is used to release one chunk of memory which was allocated using new.

75)Does an abstract class in C++ need to hold all pure virtual functions?

Ans: Not necessarily, a class having at least one pure virtual function is abstract class too.

76)Is it legal to assign a base class object to a derived class pointer?

Ans: No, it will be error as the compiler fails to do conversion.

77)What happens if an exception is thrown outside a try block?

Ans: The program shall quit abruptly.

78)Are the exceptions and error same?

Ans: No, exceptions can be handled whereas program cannot resolve errors.

79)What is function overriding?

Ans: Defining the functions within the base and derived class with the same signature and name where the base class function is virtual.
What is function overriding in c

80)Which function is used to move the stream pointer for the purpose of reading data from stream?

Ans: seekg()

81)Which function is used to move the stream pointer for the purpose of writing data from stream?

Ans: seekp()

82)Are class functions taken into consideration as part of the object size?

Ans: No, only the class member variables determines the size of the respective class object.

83)Can we create and empty class? If so what would be the size of such object/

Ans: We can create an empty class and the object size will be 1.

84)What is std?

Ans: Default namespace defined by C++.

85)What is the full form of STL?

Ans: Standard template library.

86)What is cout?

Ans: cout is the object of ostream class. The stream cout is by default connected to console output device.

87)What is cin?

Ans: cin is the object of istream class. The stream cin is by default connected to console input device.

88)What is the use of the keyword using?

Ans: It is used to specify the namespace being used in.

89)If a pointer declared for a class, which operator can be used to access its class members?

Ans: Arrow (->) operator can be used for the same.

90)What is difference between including the header file with-in angular braces < > and double quotes “ “

Ans: If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path.



91)S++ or S=S+1, which can be recommended to increment the value by 1 and why?

Ans: S++, as it is single machine instruction (INC) internally.

92)What is the difference between actual and formal parameters?

Ans: The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.

93)What is the difference between variable declaration and variable definition?

Ans: Declaration associates type to the variable whereas definition gives the value to the variable.

94)Which key word is used to perform unconditional branching?

Ans: goto.

95) Is 068 a valid octal number?

Ans: No, it contains invalid octal digits.

96)What is the purpose of #undef preprocessor?

Ans: It will be used to undefine an existing macro definition.

97)Can we nest multi line comments in a C++ code?

Ans: No, we cannot.

98)What is a virtual destructor?

Ans: A virtual destructor ensures that the objects resources are released in the reverse order of the object being constructed w.r.t inherited object.
What is a virtual destructor in c

99)What is the order of objects destroyed in the memory?

Ans: The objects are destroyed in the reverse order of their creation.

100)What is a friend class?

Ans: A class members can gain accessibility over other class member by placing the class declaration prefixed with the keyword friend in the destination class.

Also Read Java Interview Questions And Answers 
PHP Interview Questions And Answers 

Comments

Popular posts from this blog

How To give Self introduction in an Interviews | Introduce yourself Self Introduction Sample

How To give Self introduction in Interviews Introduce yourself H ello guys today we are going to learn how to introduce yourself in interviews in English. "How to give self introduction in an Interview"? The same question can be asked in different types like Introduce yourself,describe yourself, tell something about yourself..etc. It is very important that you have to present it properly in front of interviewer. Your presentation of your personal introduction will decide your job. So here we are Going to see some samples of " How to introduce yourself in an interview" . Steps To Follow : Step :1 First of all be polite to wish the interviewers. You can wish by saying Good morning, Good afternoon and Good evening etc. Likewise if you are an experienced candidate, you can shake hand with the interviewer with a firm smile on your face. Step: 2 Tell your full name and where are you from. Rather than mentioning your place name only, you should a...

Java collections interview questions and answers For Fresher's and experienced | Interview Tips

Hello guys today we are going to learn about Java questions and answers. This will help you to get success in your interviews. 1) List any Five features of Java? Ans: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded. 2) Why is Java Architectural Neutral? Ans: It’s compiler generates an architecture-neutral object file format, which makes the compiled code to be executable on many processors, with the presence of Java runtime system                           Java Architectural Neutral 3) How Java enabled High Performance? Ans: Java uses Just-In-Time compiler to enable high performance. Just-In-Time compiler is a program that turns Java bytecode, which is a program that contains instructions that must be interpreted into instructions that can be sent directly to the processor. 4) Why Java is considered dynamic? Ans: It is designed to adapt t...

How To give Self introduction In An Interviews With Examples

How To give Self introduction In An Interviews With Examples Most of the people don't know how to give Self introduction in an Interviews in proper way. The same question can be asked in different types like Introduce yourself,describe yourself, tell something about yourself..etc. It is very important that you have to present it properly in front of interviewer. Your presentation of your personal introduction will decide your job. Self introduction is the most important part in any interview. So today we are going to learn how to give Self introduction in an  Interviews with some examples. Interview Introduction  Example 1:  Good morning sir/madam, My name is Murali from Hyderabad . I have a great interest towards computer and its related works. So regularly reading blogs, watching videos and reading magazines to enhance my knowledge and skill in the field of computer. Presently i am continuing my B-Tech in the stream of computer science from C ...