 
                 
                InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. | Define the term looping. | 
| Answer» Looping means execution of a statement or block of statements repeatedly until the condition is true. | |
| 2. | What is post-tested looping statement? | 
| Answer» The loop evaluates the condition at the end of the looping structure and selects a statement for execution repeatedly. Therefore it is called a post-tested looping statement. | |
| 3. | Explain the working of ‘for’ loop structure. | 
| Answer» A ‘for’ loop is a repetition control structure, that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax: The syntax of a ‘for’ loop in C++ is: for (initialization; condition; increment/decrement) { statement(s); } The working of a ‘for’ loop: 1. The initialization step is executed first, and only once in the beginning. It is used to declare and initialize loop control variables. 2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop. 3. After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment/ decrement statement and update any ‘loop’ control variables. 4. Then the condition is evaluated again. If it is true, the ‘ loop’ executes and the process repeats itself (body of ‘loop’, then increment step, and then again condition). After the condition becomes false, the ‘for’ loop terminates. Example: #include<iosteam> int main() { //for loop execution for(int a = 10; a <20; a = 1+ 1) { cout<<"value of a:"<<a<<end1; } return 0; } When the above code is compiled and executed, it produces following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 | |
| 4. | Explain ‘while’ loop structure with an example. | 
| Answer» A ‘while’ loop statement repeatedly executes a statement or sequence of statements written in the flower brackets as long as a given condition returns the value ‘true’. Syntax: The syntax of a ‘while’ loop in C++ is: while(condition) { statement (s) ; } Here the condition may be any expression, and for true is any non zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. During the first attempt, when the condition is tested and the result is false, the loop body will be skipped and the first statement after the ‘while’ loop will be executed. Example: #include<iostream> int main () { int a = 10; while (a<15) { cout <<"value of a:"<<a<<end1; a++; } return 0; } When the above code is executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 | |
| 5. | Give the difference between ‘go to’ statement and ‘continue’ statement. | 
| Answer» The ‘go to’ statement transfers/jump control from one part of the program some other part. The ‘continue’ statement is used in loops and causes a program to skip the rest of the body of the loop. | |
| 6. | Give the difference between ‘break’ and ‘exit()’ statements. | 
| Answer» The ‘break’ statement can be used to terminate a repeated structure (loops) such as ‘while’, do-while and for and multi-branching statements like ‘switch’. The execution of a program can be stopped at any point with ‘exit ( )’. | |
| 7. | Write the syntax of ‘do-while’ loop. | 
| Answer» Syntax of ‘do-while’ loop do { statements; } while (condition); | |
| 8. | Write the syntax of ‘while’ loop. | 
| Answer» Syntax of ‘while’ loop: while(condition) { statement(s); } | |
| 9. | Write the syntax of if-else-if statement. | 
| Answer» Syntax : if(expression) { statements } else if(expression) { statements } else if(expression) { statements } | |
| 10. | What are selection statements? Give the different types of selection statements. | 
| Answer» Selection statements are used to execute certain block of statements by evaluating the conditions. The different selection statements are 1. ‘if’ statement 2. ‘if-else’ statement 3. ‘if-else-if’ (nested) statement 4. ‘switch’ statement | |
| 11. | Write the syntax of ‘if-else’ statement. | 
| Answer» If (condition) { statement 1; .... statement n; else { statement 1; ..... statement n; } | |
| 12. | Give the syntax of ‘if’ statement. | 
| Answer» The syntax of if statement is if (condition) { statement 1; statement 2; statement n; } | |
| 13. | When is ‘for’ used? | 
| Answer» If the programmer knows the exact number of repetitions to be carried out among the set of statements then ‘for’ conditional statement is used in a program. | |
| 14. | What is the purpose of ‘if’ statement? | 
| Answer» The ‘if’ statement is used to decide on whether a statement or a block should be executed or not. | |
| 15. | What is a block? | 
| Answer» A group of statements separated by semicolons and grouped together and enclosed within brackets marked with { } is called a block. | |
| 16. | Write the classification of control statements. | 
| Answer» The control statements are classified as 1. Selection statements 2. Iterative statements 3. Jump statements. | |
| 17. | What are control statements? | 
| Answer» Control statements are elements in the program that can control and alter the sequence of flow of constructions in any program execution. | |
| 18. | Why is the ‘exit() library’ function used? | 
| Answer» The execution of a program can be stopped at any point with ‘exit ( )’ and a status code can be informed to the calling program. | |
| 19. | What is meant by a compound statement? | 
| Answer» A compound statement is a grouping of statements enclosed within a pair of braces ({ }), in which each individual statement ends with a semicolon. | |
| 20. | What is the function of ‘continue’ statement? | 
| Answer» The ‘continue’ statement is used in loops and causes a program to skip the rest of the body of the loop. | |
| 21. | What is the use of ‘goto’ statement? | 
| Answer» The ‘go to’ statement transfers/jump control from one part of the program to some other part. | |
| 22. | What is ‘nested if’? | 
| Answer» An if statement nestling inside another ‘if’ statement is termed as ‘a nested if’ statement. | |
| 23. | What is the other name of ‘if’ statement? | 
| Answer» The other name of ‘if’ statement is one-way branching statement. | |
| 24. | Give the name of multi-branch selection statement. | 
| Answer» The multi-branch selection statement is called as ‘switch-case’ statement. | |
| 25. | What is the numerical equivalent of TRUE or FALSE? | 
| Answer» The numerical equivalent of TRUE is 1 and FALSE is 0. | |
| 26. | What is an ‘if-else-if’ statement? | 
| Answer» The ‘if-else if’ statement is an extension of the “if-else” conditional branching statement. When the expression in the “if’ condition is “false” another “if-else” construct is used to execute a set statement based on an expression. | |
| 27. | When is ‘if-else’ statement used? | 
| Answer» ‘If else’ statement is a conditional branching statement used to execute a set of codes when the condition is true, otherwise executes a different set of the codes in the “else” part. | |
| 28. | What is the other name of ‘if-else’ statement? | 
| Answer» The other name of ‘if-else’ statement is two-way branching. | |
| 29. | Give the possible output of condition evaluation. | 
| Answer» The possible output of condition evaluation is either TRUE or FALSE. | |
| 30. | What is the purpose of ‘break’ statement? | 
| Answer» The ‘break’ statement can be used to terminate a repeated structure (loops) such as ‘while’, do-while ‘and’ ‘for’ and multi-branching statements like ‘switch’. | |
| 31. | What is nesting of loop? | 
| Answer» If the loop appears inside the body of another loop it is called a nested loop. | |
| 32. | Give the purpose of the switch statement. | 
| Answer» The ‘switch’ statement is used in C++ for testing whether a variable is equal to any one of a set of values. | |
| 33. | What are control statements? How are they classified? | 
| Answer» Control statements are elements in the program that control the flow of program execution. Control statements are classified as, 1. Selection statements 2. Iterative statements 3. Jump statements | |
| 34. | Write the syntax of‘switch-case’ statement. | 
| Answer» Syntax : switch(expression) { case constant 1 : statements break case constant 2 : statements break . . default statements } | |
| 35. | Write the comparison between ‘if-else’ and ‘switch-case’ statement. | 
| Answer» The ‘if-else’ statements permit two-way branching, whereas ‘switch’ statement permits multiple branching. | |
| 36. | Write a short note on ‘switch-case’ statement. | 
| Answer» A ‘switch’ statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax: The syntax for a switch statement: switch(expression) { case constant-expression : statement(s); break; //optional case constant-expression : statement(s); break; //optional default : statement(s); //Optional } The following rules apply to a switch statement: 1. The expression used in a ‘switch’ statement must have an integer or enumerated type. 2. We can have any number of case statements within a ‘switch’. Each case is followed by the value to be compared to, and a colon. 3. The constant-expression for a case must be the same data type as the variable in the ‘switch’, and it must be a constant or a literal. 4. When the variable being switched on is equal to a case, the statements following that case will execute until a ‘break’ statement is reached. 5. When a ‘break’ statement is reached, the ‘switch’ terminates, and the flow of control jumps to the next line following the ‘ switch ’ statement. 6. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a ‘break’ is reached. 7. If no conditions match, then the code under the default statement is executed. 8. The default case is used for performing a task when none of the cases is ‘true’. 9. No break is needed in the default case. | |
| 37. | Explain the working of ‘ if’ statement and ‘if-else’ statement with examples. | 
| Answer» Example for ‘if’ statement: # include <iostream.h> void main () { int a = 100; int b = 200; if (b > a) { cout <<"B is greater than A"; } } Result: B is greater than A In the above example the condition in the if statement returns a true value and so the text “B is greater than A” is displayed. Example for ‘if else’ statement: #include <iostream.h> void main () { int a = 10; int b = 20; if(a>b) { cout <<"A is greater than B"; } else { cout <<"B is greater than A"; } Result: B is greater than A. In the above example, the “if’ condition is false so the code in the “else” part is executed. | |
| 38. | Explain the working of if-else if statement with a suitable example. | 
| Answer» The ‘if-else if’ statement is an extension of the “if-else” conditional branching statement. When the expression in the “if’ condition is “false” another “if-else” construct is used to execute a set of statements based on the expression. Syntax: if(expression) { statements } else if(expression) { statements } else if(expression) { statements }\ Example: for ‘if else if’ statement #include<iostream.h> int main (void) { int per; cout<<"Enter your Percentage::"; cin>>per; If(per>=80) { cout<<"passed with Distinction"<<endI; } else if(per>=60) { cout <<"First Class"<<end1; } else if(per>=50) } cout<<"Second Class"<<end1; } else { cout<<"Third Class"<<end1; } return 0; } Result: Enter your Percentage::60 First Class In the above example, the ‘if else if’ statement is used to check multiple conditions based on the percentage of marks got, as input. The user entered percentage as 60, then first condition evaluated and returned with false. This resulted in checking for second if condition and resulted in true which give the print “first class”. | |
| 39. | Name the post-tested looping statement. | 
| Answer» The ‘do-while’ statement is a post-tested looping statement. | |
| 40. | Which structure is called a fixed-execution looping statement? | 
| Answer» The ‘for’ looping statement is called a fixed-execution looping statement. | |
| 41. | Why is ‘while’ loop called a pre-tested looping statement? | 
| Answer» The ‘while’ loop evaluates the condition in the beginning itself, to select a suitable statement for execution repeatedly. Therefore it is called a pretested looping statement. | |