Mastering operators and if statements is essential for any programmer seeking to excel in the C++ language. These fundamental constructs form the backbone of logical decision-making and data manipulation in C++, enabling developers to build robust and efficient code. Moreover, a solid understanding of these concepts is often put to the test in job interviews, where output questions play a significant role in assessing a candidate’s programming skills.
This article aims to provide aspiring programmers and job seekers with a comprehensive set of practice problems that focus specifically on operators and if statements in C++. By simulating interview-style output questions, this collection of exercises will challenge your problem-solving abilities and help you build the confidence needed to excel in real-world programming scenarios.
The practice problems presented here cover a wide range of scenarios, from simple to complex, and address various aspects of operators and if statements, such as arithmetic operators, relational operators, logical operators, conditional statements, and nested if statements. Each problem is carefully crafted to emphasize different aspects of these concepts, allowing you to gain a thorough understanding of their application in practical programming situations.
To help you make the most of these practice problems, each solution is accompanied by detailed explanations and code walkthroughs. This way, you can not only solve the problems but also understand the underlying principles and reasoning behind each solution.
Whether you are preparing for a technical interview or simply aiming to strengthen your programming skills, this article will serve as an invaluable resource. Let’s dive in and master operators and if statements in C++ through interview-style output questions!
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ float i = 5.25; double j = 5.25; if(i == j) cout<<"Study"; else cout<<"Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No Output |
Answer : Option a) Study. In this code, the variables i and j are initialized with different data types. i is of type float, while j is of type double. Although both variables hold the same value, they have different representations due to the differences in precision between float and double data types. When the comparison i==j is performed, the C++ language performs implicit type conversion to promote float to double. This is because in C++, the promotion of data types follows a hierarchy, with float being lower than double. As a result, the comparison i==j becomes 5.25==5.25, with both operands now being of type double. Since the values are equal, the condition in the if statement evaluates to true. |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main() { cout<<sizeof(2.5); cout<<sizeof(2); cout<<sizeof('A'); return 0; } |
a) 8 4 2 |
b) 8 4 1 |
c) 4 4 1 |
d) Error |
Answer : option a) 8 4 2 |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ int n = - -2; cout<<n; return 0; } |
a) -2 |
b) 2 |
c) Error |
d) No Output |
Answer : option b) 2 : In this code, the variable ‘n’ is assigned the value ‘– -2′. At first glance, this expression might appear confusing due to the consecutive negative signs. However, this is a valid construct in C++ that takes advantage of the unary negation operator (‘-‘). Let’s break down the expression step by step: 1. The first negative sign (‘-‘) is the unary negation operator. It negates the value that follows it. When the code is executed, the expression ‘- -2’ is evaluated as follows: 1. The first negative sign (‘-‘) negates the value ‘-2’, resulting in ‘2’. As a result, the expression ‘- -2’ is effectively evaluated as ‘2’, and the value ‘2’ is assigned to the variable ‘n’. When the code reaches the “cout << n;” statement, it will output the value of “n”, which is “2”. Therefore, the output of the code will be “2.” |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ int n = --2; cout<< n; return 0; } |
a) 1 |
b) 2 |
c) -1 |
d) Error |
Answer : d) Error, In C++, the unary decrement operator (“–“) can only be applied to variables, not to constant values or literals. The compiler expects an lvalue (modifiable value) on the left side of the decrement operator. |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main() { float a = 5.0; cout << "Result is " << (24 / 5) * a; return 0; } |
a) 20.0 |
b) 24 |
c) 24.0 |
d) 20 |
Answer : 20.0 |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ int x=0; if(x == x) cout<<"Study"; else cout<<"Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No Output |
Answer : option a) Study, In this code, the variable x is declared and initialized with the value 0. The if statement checks the condition x==x, which compares the value of x to itself. Since x is equal to x (both are 0 in this case), the condition evaluates to true. When the condition in the if statement evaluates to true, the code inside the corresponding if block is executed. In this case, the statement cout<<“Study” is executed. Therefore, the output of the code will be “Study”, which is printed to the console. |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ int x=0; if(x) cout<<"Study"; else cout<<"Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No Output |
Answer : b) Trigger, In this code, the variable x is declared and initialized with the value 0. The if statement checks the condition x. In C++, the value of 0 is considered as false in a boolean context, while any non-zero value is considered true. Since x has the value 0, which is equivalent to false, the condition x evaluates to false. When the condition in the if statement evaluates to false, the code inside the corresponding else block is executed. In this case, the statement cout<<“Trigger”; is executed. Therefore, the output of the code will be “Trigger”. |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main(){ if(true) cout<<"Study"; else cout<<"Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No Output |
Answer : Option a) Study, In this code, there is an if statement with the condition true . Since the condition is explicitly set to true , the code inside the corresponding if block will always be executed.But if the above question asked in C-Language then the answer will be “Trigger” as there is no boolean literal “true”. |
Question : What will be the output of the following code : |
#include<iostream.h> using namespace std; int main(){ int i = 5, j = 5; if(i == j); cout<<"Study"; else cout<<"Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No Output |
Answer : Option c) Error. Misplaced else (due to semicolon is present at the end of if statement), however if there is no else part in the above code, then you will find “Study” as an output. |
Question : What will be the output of the following code : |
#include<iostream> using namespace std; int main() { int i = 2; if (i == (1, 2)) cout << "Study"; else cout << "Trigger"; return 0; } |
a) Study |
b) Trigger |
c) Error |
d) No output |
Answer : option a) Study, In this code, the variable i is declared and initialized with the value 2.The if statement checks the condition i==(1,2). In this condition, the comma operator (,) is used between the values 1 and 2. The comma operator evaluates both expressions but returns the value of the rightmost expression, which is 2 in this case. Therefore, the condition i==(1,2) is equivalent to i==2. Since i has the value 2, which is equal to 2, the condition i==2 evaluates to true. When the condition in the if statement is true, the code inside the corresponding if block is executed. In this case, the statement cout<<“Study” is executed. |
In conclusion, tackling output-based problems on operators and if statements provides a valuable opportunity to deepen your understanding and refine your problem-solving skills. Through the challenges presented in this article, you have explored various scenarios, tested your knowledge, and honed your ability to predict and analyze program outputs. By mastering these fundamental concepts, you are better equipped to excel in technical interviews and real-world programming scenarios. Remember to stay connected for more interesting questions and practice problems that will further expand your skills. Keep exploring, stay curious, and continue your journey towards becoming a confident and proficient programmer.