228k views
2 votes
Java language.

Answer all three parts
a) What will be the output of the following program?
public class Sum {
public static void main(String[] args) {
for( int i=0, sum =0; i<=5; i++) {
sum = sum + i;
}
System.out.println(sum);
}
}
b) The following program gets user input and prints it. Say the user enters 1600 Holloway Ave. What will be the output of the program?
import java.lang.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userInput = input.next();
System.out.println(userInput);
}
}
c) What will the following expression evaluate to?
2 * 2 - 3 < 2 && 5 - 2 < 5

User Akj
by
7.8k points

1 Answer

6 votes

Final answer:

a) The output will be 15. b) The output will be '1600'. c) The expression evaluates to false.

Step-by-step explanation:

a) The output of the program will be 15. The program uses a for loop to iterate over the values of i from 0 to 5. On each iteration, the value of i is added to the variable sum. So, the sum will be 0 + 1 + 2 + 3 + 4 + 5 = 15.

b) The output of the program will be '1600'. The program uses a Scanner object to get user input and stores it in a String variable called userInput. Then, it prints the value of userInput.

c) The expression 2 * 2 - 3 < 2 && 5 - 2 < 5 will evaluate to false. Let's break it down: 2 * 2 - 3 = 1 < 2 evaluates to true, and 5 - 2 < 5 = 3 < 5 evaluates to true. However, the && operator requires both conditions to be true for the overall expression to be true. Since one condition is false, the expression will be false.

User Ayush Seth
by
8.5k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.