C PROGRAMMING Interview Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

C PROGRAMMING

1. What is “this”s pointer?

The pointer is a pointer accessible only within the member functions of a class, struct, or union
type. It points to the object for which the member function is called. Static member functions do
not have a this pointer.

2. What is NULL pointer? 

 NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should
initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we
should make a pointer NULL when memory pointed by it is deallocated in the middle of a
program.

3. What is Dangling pointer?

Dangling Pointer is a pointer that doesn’t point to a valid memory location. Dangling pointers
arise when an object is deleted or deallocated, without modifying the value of the pointer, so that
the pointer still points to the memory location of the deallocated memory.

4. What is difference between i++ and ++i?

a) The expression ‘i++’ returns the old value and then increments i. The expression ++i
increments the value and returns new value.
b) Precedence of postfix ++ is higher than that of prefix ++.
c) Associatively of postfix ++ is left to right and associatively of prefix ++ is right to left.
d) In C++, ++i can be used as l-value, but i++ cannot be. In C, they both cannot be used as l-
value.

5. What is the difference between Call by Value and Call by Reference?

When using Call by Value, you are sending the value of a variable as parameter to a function,
whereas Call by Reference sends the address of the variable. Also, under Call by Value, the
value in the parameter is not affected by whatever operation that takes place, while in the case of
Call by Reference, values can be affected by the process within the function.

6. What do you mean by a sequential access file?


a) While writing programs that store and retrieve data in a file, it is possible to designate that file
into different forms. 
b) A sequential access file is such that data are saved in sequential order: one data is placed into
the file after another. 
c) If you want to access a particular data within the sequential access file, data has to be read -
one data at a time, until you reach the data you want.

7. Are bit fields portable?

A) No, Bit fields are not portable. 


b) Since Bit fields cannot span machine words and the number of bits in a machine word is
different on different machines, a particular program using bit fields might not compile on some
machines.
c) One should avoid using bit fields except when the machines can directly address bits in
memory and the compiler can generate code.

8. Explain high-order and low-order bytes.

a) The numbers are written from left to right in the decreasing order of significance. Similarly,
the bits in a byte of computer memory can be considered as digits of a number written in base 
b) the byte holding the least significant 8 bits is called the low-order byte. 
c) The byte holding the most significant 8 bits is the most significant byte, or high- order byte.

9. Why are all header files not declared in every C program?

a) Declaring all header files in every program would lead to increase in the overall file size and
load of the program. It is not a good programming. 
b) The choice of header files that you want to declare in the program depends on the
commands/functions you want to use in the program. Each header file contains different
commands and functions. So we use only the files relevant to our program.

10. How many levels deep can include files be nested?

a) As such, there is no limit to the number of levels of nested include files you can have but your
compiler might run out of stack space while trying to include an inordinately high number of
files. This number depends on your hardware configuration and compiler.

b) Although it is legal to nest include files yet you must avoid it. An include level should be
created only where it is required and makes sense, like creating an include file that has an
#include statement for each header required by the module you are working with.
11. How does placing some code lines between the comment symbols help in debugging the
code?

a) Placing comment symbols /* */ around a code isolates some code that the coder believes
might be causing the errors in the program, without deleting it. 
b) If the code is correct, you can just remove the comment symbols, without needing to retype it. 
c) If it is wrong, you can just remove it.

12. Differentiate between functions getch() and getche().

a) Both functions accept a character input value from the user. 


b) When getch() is used, the key that was pressed will not appear on the screen. It is
automatically captured and assigned to a variable. 
c) While when getche() is used, the key that was pressed by the user appears on the screen and is
assigned to a variable.

13. Are comments included during the compilation stage and placed in the EXE file as
well?

a) No, comments encountered by the compiler are disregarded.


b) Their only purpose is guidance and ease of programming. They have no effect on the
functionality of the program.

14. Explain the meaning of keyword 'extern' in a function declaration.

a) 'Extern' modifier in a method declaration implies that the method is implemented externally. 
b) The program doesn't reserve any memory for a variable declared as 'extern'. 
c) A variable that is required to be used in every file of the project is declared globally in one file
and not inside any function.
d) 'Extern' declaration of that variable is added to a header file not included in all others.

15. Explain "Bus Error".

a) It is a fatal error in the execution of a machine language instruction. 


b) It occurs because the processor detects an abnormal condition on its bus. 

The causes may be:


- Invalid address alignment. 
- Accessing a physical address that does not correspond to any device. 
- Other device specific hardware error.

16. What is spaghetti programming?


Spaghetti programming refers to codes that tend to get tangled and overlapped throughout the
program. This unstructured approach to coding is usually attributed to lack of experience on the
part of the programmer. Spaghetti programming makes a program complex and analyzing the
codes difficult, and so must be avoided as much as possible.

17.  How do you generate random numbers in C?


Random numbers are generated in C using the rand() command. For example: anyNum = rand()
will generate any integer number beginning from 0, assuming that anyNum is a variable of type
integer.

18. How do you declare a variable that will hold string values?

The char keyword can only hold 1 character value at a time. By creating an array of characters,
you can store string values in it. Example: “char My Name [50]; ” declares a string variable
named My Name that can hold a maximum of 50 characters.

19. Which function in C can be used to append a string to another string?

The strcat function. It takes two parameters, the source string and the string value to be appended
to the source string.

20. What is wrong in this statement?  scanf(“%d”,whatnumber);

An ampersand & symbol must be placed before the variable name what number. Placing &
means whatever integer value is entered by the user is stored at the “address” of the variable
name. This is a common mistake for programmers, often leading to logical errors.

21. Can the “if” function be used in comparing strings?


No. “if” command can only be used to compare numerical values and single character values.
For comparing string values, there is another function called strcmp that deals specifically with
strings.

22. How do you determine the length of a string value that was stored in a variable?

To get the length of a string value, use the function strlen(). For example, if you have a variable
named FullName, you can get the length of the stored string value by using this statement: I =
strlen(FullName); the variable I will now have the character length of the string value.

23. What does void keyword implies when used to the left of function name in function
definitions?

Void to the left of function name in function definition indicates that function will not return a
value.

24. In C functions, what is the easiest way to pass arrays as arguments?

Passing by reference is the easiest way to pass arrays as arguments in C functions.

25. What are macros and why they are used?

In programming terms, macros can be defined as short statements that are actually expanded into
lengthier set of statements during execution.

Macros in C save a lot of programming time (though there is no direct impact on execution
time.)
OBJECT ORIENTED
PROGRAMMING (C++)
1. What is the full form of OOPS?
Object Oriented Programming System.

2. What is a class?
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?
An instance of the class is called as object.

4. List the types of inheritance supported in C++.


Single, Multilevel, Multiple, Hierarchical and Hybrid.

5. What is the role of protected access specifier?


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?
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?
Abstraction refers to hiding the internal implementation and exhibiting only the necessary
details.

8. What is inheritance?
Inheritance is the process of acquiring the properties of the existing 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.


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?


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.
11. What is a storage class?
Storage class specifies the life or scope of symbols such as variable or functions.

12. Mention the storage classes names in C++.


The following are storage classes supported in C++

auto, static, extern, register and mutable

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


A constant class object’s 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.


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?


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++?


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++?


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?


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.


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++.
wchar_t

21. What are/is the operator/operators used to access the class members?
Dot (.) and Arrow ( -> )

22. Can we initialize a class/structure member variable as soon as the same is defined?
No, Defining a class/structure is just a type definition and will not allocated memory for the
same.

23. 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

24. Can a program be compiled without main() function?


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

25. Where an automatic variable is stored?


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

26. What is a container class?


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

27. What is a token?


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

28. What is a preprocessor?


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

29. What are command line arguments?


The arguments which we pass to the main() function while executing the program are called as
command line arguments. The parameters are always strings held in the second argument
(below in args) of the function which is array of character pointers. First argument represents
the count of arguments (below in count) and updated automatically by operating system.

main( int count, char *args[]) {


}

What are the different ways of passing parameters to the functions? Which to use when?
 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.

30. What is reminder for 5.0 % 2?


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

31. Which compiler switch to be used for compiling the programs using math library
with g++ compiler?
Opiton –lm to be used as > g++ –lm <file.cpp>

Can we resize the allocated memory which was allocated using ‘new’ operator?
No, there is no such provision available.

32. Who designed C++ programming language?


Bjarne Stroustrup.

33. Which operator can be used to determine the size of a data type/class or
variable/object?
sizeof

How can we refer to the global variable if the local and the global variable names are same?
We can apply scope resolution operator (::) to the for the scope of global variable.

34. What are valid operations on pointers?


The only two permitted operations on pointers are

 Comparision ii) Addition/Substraction (excluding void pointers)


35. What is recursion?
Function calling itself is called as recursion.

36. What is the first string in the argument vector w.r.t command line arguments?
Program name.

37. What is the maximum length of an identifier?


Ideally it is 32 characters and also implementation dependent.

38. What is the default function call method?


By default the functions are called by value.

You might also like