T4Tutorials .PK

VU Past Papers CS201- Midterm Solved Questions on Data Types, Loops, and Control Structures

Q#1: Why is it important to declare variables as double in the program?
Answer: Declaring variables as double ensures more accurate results, especially in calculations involving division. Using decimal values such as 15.0 / 100 prevents integer division and allows the program to produce precise results with decimal points.


Q#2: Explain the significance of using braces in an if/else structure according to the provided tips.
Answer: Braces { } define the block of statements that belong to the if or else condition. Using braces improves code readability and prevents logical errors that may occur if multiple statements are written without braces.


Q#3: What is the purpose of the while loop in the program’s sample execution?
Answer: The while loop repeatedly executes a set of statements as long as a specified condition remains true. In the sample program, it is used to calculate the sum of integers from 1 to 1000 by repeatedly adding each number to the variable sum.


Q#4: How does the program handle potential overflow conditions when summing integers?
Answer: The program explains that overflow can occur because integers have limited memory space (for example, 32 bits). If the calculated value exceeds this limit, incorrect values may be stored. The program does not explicitly handle overflow but highlights the importance of choosing appropriate data types and being careful when working with large numbers.

Exit mobile version