Important Introduction to Programming Short Questions
Chapter: 5
Question 1:
What happens if you write if (x = 2) instead of if (x == 2) in C?
Answer:
If you write if (x = 2), it assigns the value 2 to x and the condition always evaluates to true unless x is zero, causing a logical error.
Chapter: 5
Question 2:
What is a flowchart in programming?
Answer:
A flowchart is a visual representation of a program’s logic using symbols and arrows to show the sequence of operations.
Chapter: 5
Question 3:
How does the 'if/else' structure improve decision making in programming?
Answer:
The 'if/else' structure allows for two different paths of execution based on whether the condition is true or false, ensuring proper decision-making.
Chapter: 5
Question 4:
What does the following C code do? if (AmerAge > AmaraAge) { cout << "Amer is older than Amara"; } else { cout << "Amer is younger than Amara"; }
Answer:
This code compares the ages of Amer and Amara, printing a message indicating whether Amer is older or younger.
Chapter: 5
Question 5:
Why is it important to use braces in an 'if/else' structure?
Answer:
Using braces in an 'if/else' structure ensures that all intended statements are executed as part of the condition, avoiding logical errors.
Chapter: 5
Question 6:
What is the benefit of initializing variables with a value like 0 in C programming?
Answer:
Initializing variables with a value like 0 ensures that calculations using these variables do not result in undefined or erroneous results.
Chapter: 5
Question 7:
What is the result of the expression (age > 18) && (height > 6) if age is 17 and height is 7?
Answer:
The result is false because the first condition (age > 18) is false, causing the entire expression to evaluate as false.
Chapter: 5
Question 8:
Why should you be careful when using logical operators in C?
Answer:
You should be careful because logical operators can create complex expressions where order and combination of conditions affect the outcome.
Chapter: 5
Question 9:
What does the program do if the user inputs AmerAge < AmaraAge in the example program?
Answer:
The program prints "Amer is younger than Amara" when Amer's age is less than Amara's.
Chapter: 5
Question 10:
What does a truth table represent in logical operations?
Answer:
A truth table shows all possible combinations of input values and their corresponding output for logical operators like &&,