Page 234 - ComputerScience_Class_11
P. 234
Based on the given case, answer the following questions:
1. What was the error Ananya encountered while running her program?
a. She used an undefined variable in her calculation. b. She tried to use a local variable outside its scope.
c. The display() method was missing. d. She forgot to import a necessary library.
2. What change did Ananya need to make to fix the error?
a. Declare the perimeter variable as a data member of the class.
b. Move the display() method to another class.
c. Use the perimeter variable only in the calculate() method.
d. Remove the calculate() method entirely.
3. Which type of variable is perimeter in Ananya's program?
a. Global variable b. Local variable
c. Data member d. Constant
4. What would happen if Ananya had declared perimeter as a local variable inside the display() method instead of the calculate()
method?
a. The program would work correctly and display both area and perimeter.
b. The program would still throw an error because perimeter would be out of scope in display().
c. The program would display only the perimeter but not the area.
d. The program would not display any output.
Answers
1. b 2. a 3. b 4. b
G. Identify the error in the given codes and rewrite the correct code:
1. class Main {
public static void main(String[] args) {
int a;
if(a=1)
a==100;
else If(a=2)
a=1000;
}
}
Ans. class Main {
public static void main(String[] args) {
int a=1;
if(a==1){
a=100;
System.out.println("a is 100");}
else{
a=1000;
System.out.println("a is 1000");}
}
}
2. switch(m){
case 0:
x=x+2;
System.out.println("X=" x);
232 Touchpad Computer Science (Ver. 3.0)-XI

