IT430_JavaScripting (for MBA 3rd semester students)
Page 1 of 1
IT430_JavaScripting (for MBA 3rd semester students)
Questions
1. Operators may be used in building expressions, which compute values.
2. Expressions are the core components of statements.
3. Statements may be grouped into blocks.
4. The following code snippet is an example of a compound expression.
1 * 2 * 3
5. Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a semicolon.
6. A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
Ex:
Identify the following kinds of expression statements:
* aValue = 8933.234; // assignment statement
* aValue++; // increment statement
* System.out.println("Hello World!"); // method invocation statement
* Bicycle myBike = new Bicycle(); // object creation statement
Objective Questions
1. The _____ statement allows for any number of possible execution paths.
1.if
2.while
3.switch
4.none
2. Question: How do you write an infinite loop using the for statement?
Answer:
for ( ; ; ) {
}
3. Question: How do you write an infinite loop using the while statement?
Answer:
while (true) {
}
Subjective Questions
1. Consider the following code snippet.
2. if (aNumber >= 0)
3. if (aNumber == 0) System.out.println("first string");
4. else System.out.println("second string");
5. System.out.println("third string");
a. Question: What output do you think the code will produce if aNumber is 3?
Solution:
second string
third string
b. Question: Write a test program containing the previous code snippet; make aNumber 3. What is the output of the program? Is it what you predicted? Explain why the output is what it is. In other words, what is the control flow for the code snippet?
Solution: NestedIf
second string
third string
3 is greater than or equal to 0, so execution progresses to the second if statement. The second if statement's test fails because 3 is not equal to 0. Thus, the else clause executes (since it's attached to the second if statement). Thus, second string is displayed. The final println is completely outside of any if statement, so it always gets executed, and thus third string is always displayed.
c. Question: Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
Solution:
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else
System.out.println("second string");
System.out.println("third string");
d. Question: Use braces { and } to further clarify the code and reduce the possibility of errors by future maintainers of the code.
Solution:
if (aNumber >= 0) {
if (aNumber == 0) {
System.out.println("first string");
} else {
System.out.println("second string");
}
}
System.out.println("third string");
1. Operators may be used in building expressions, which compute values.
2. Expressions are the core components of statements.
3. Statements may be grouped into blocks.
4. The following code snippet is an example of a compound expression.
1 * 2 * 3
5. Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a semicolon.
6. A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
Ex:
Identify the following kinds of expression statements:
* aValue = 8933.234; // assignment statement
* aValue++; // increment statement
* System.out.println("Hello World!"); // method invocation statement
* Bicycle myBike = new Bicycle(); // object creation statement
Objective Questions
1. The _____ statement allows for any number of possible execution paths.
1.if
2.while
3.switch
4.none
2. Question: How do you write an infinite loop using the for statement?
Answer:
for ( ; ; ) {
}
3. Question: How do you write an infinite loop using the while statement?
Answer:
while (true) {
}
Subjective Questions
1. Consider the following code snippet.
2. if (aNumber >= 0)
3. if (aNumber == 0) System.out.println("first string");
4. else System.out.println("second string");
5. System.out.println("third string");
a. Question: What output do you think the code will produce if aNumber is 3?
Solution:
second string
third string
b. Question: Write a test program containing the previous code snippet; make aNumber 3. What is the output of the program? Is it what you predicted? Explain why the output is what it is. In other words, what is the control flow for the code snippet?
Solution: NestedIf
second string
third string
3 is greater than or equal to 0, so execution progresses to the second if statement. The second if statement's test fails because 3 is not equal to 0. Thus, the else clause executes (since it's attached to the second if statement). Thus, second string is displayed. The final println is completely outside of any if statement, so it always gets executed, and thus third string is always displayed.
c. Question: Using only spaces and line breaks, reformat the code snippet to make the control flow easier to understand.
Solution:
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else
System.out.println("second string");
System.out.println("third string");
d. Question: Use braces { and } to further clarify the code and reduce the possibility of errors by future maintainers of the code.
Solution:
if (aNumber >= 0) {
if (aNumber == 0) {
System.out.println("first string");
} else {
System.out.println("second string");
}
}
System.out.println("third string");
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|