Top C-plus-plus Interview Questions
Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.
Question
9
Explanation
Explain switch statement with an example.
The switch statement is used to check variable for different values and based on the the variable different cases are executed.
Syntax:
Switch(variable)
{
case value :
-------------
-------------
break;
case value :
-------------
-------------
break;
case value :
-------------
-------------
break;
.
.
default :
-------------
-
}
>If no matching case is found then code is the default block is executed.
>Default block is optional.
>We can use a break statement to throw program control out of a switch otherwise next cases will be executed without checking their values.
Question
10
Explanation
Differentiate between if and switch statement.
1. switch is usually more compact than lots of nested if else and therefore, more readable
2. If you omit the break between two switch cases, you can fall through to the next case .No such problems with if statement
3. switch case values can only be constants.i.e. we can compare only with constant values. In if statement we can compare any type of values.
4. switch only accepts only int ,char data types and float or double.
5. switch uses only equality operator for comparision.
Question
11
Explanation
Explain arrays in C++. Write a program to read an array to 10 integer and find sum of all the elements.
An array is a group of elements of same type sharing same name and stored in a consecutive memory location. Arrays are define if we want to store multiple values of same type.
Syntax :-
Datatype arrayname[size];
# include <iostream.h>
void main()
{
int a[10] , s=0 , i;
cout << “Enter 10 no.’s”;
for( i = 0; i<=9 ; i++)
cin >> a[i];
for( i = 0; i<=9 ; i++)
cout << “Sum is ” << s;
}
Question
12
Explanation
What are string functions in C++? Give examples.
C++ provide us different library functions to perform operations on strings. These functions are declared in the header file cstring.
Examples:
1. int strlen (char *str) => returns length of the string.
2. strupr (char *str)=> convert the string into uppercase.
3. strlwr (char *str)=> convert the string into lowercase.
4. strrev (char *str)=> will reverse the contents of the string.
5. strcpy (char *dest, char *source)=> will copy source string at specified destination.
6. strcat (char *s1, char *s2)=> It joins two strings.
7. int strcmp (char *s1, char *s2)=> will compare two strings and return difference between their ASCII values.



.png)