Page 233 - ComputerScience_Class_11
P. 233
2. Consider a scenario in a smart home system where sensors detect the temperature, humidity and motion to adjust the air
conditioning and lights. How can nested conditional statements be used to control the system's behaviour? What would be the
sequence of actions if the temperature is high, but there is no motion detected?
Ans. Nested conditional statements can help ensure that actions are taken based on multiple sensor inputs. Example pseudo-code:
if(temperature > 25) {
if(motionDetected) {
turnOnAC();
turnOnLights();
} else {
turnOnAC();
turnOffLights();
}
} else {
if(humidity > 70) {
turnOnDehumidifier();
} else {
turnOffAC();
turnOffLights();
}
}
The system ensures that AC is turned on if the temperature is high and adjusts the lights based on motion detection.
E. Assertion and reasoning questions.
The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
appropriate option given below:
a. Both A and R are true and R is the correct explanation of A.
b. Both A and R are true but R is not the correct explanation of A.
c. A is true but R is false.
d. A is false but R is true.
1. Assertion: Expression statements in Java can involve calculations and modify the values of variables.
Reason: Expression statements consist of expressions that calculate certain equations and produce a result, such as a++, --b,
or even a + b.
Ans. a. Both A and R are true and R is the correct explanation of A.
2. Assertion: The Java 'for' loop is used for a fixed iteration of code.
Reason: The Java 'for' loop executes a block of code a set number of times, with initialization, condition checking and iteration
control all defined in one line.
Ans. a. Both A and R are true and R is the correct explanation of A.
3. Assertion: A local variable in Java can be accessed outside the method in which it is defined.
Reason: Local variables are only accessible within the method they are declared in and cannot be used outside that method.
Ans. d. A is false but R is true.
F. Case study-based questions.
Ananya is working on a simple Java program to calculate the area and perimeter of a rectangular plot of land. She is learning about
variables, methods and scope of variables. The user is prompted to enter the length and breadth of the plot and based on that, the
program calculates both the area and the perimeter.
While writing the program, Ananya encountered an error because she tried to use a local variable in one method that was declared
in another method. In her calculate() method, she declared a local variable perimeter to calculate the perimeter of the plot, but
when she tried to use it in the display() method, she received a compilation error. The problem arose because perimeter was a local
variable in the calculate() method and was not accessible in the display() method.
Statements and Scope 231

