CPSC 112-Midterm
Java代考 Question 1 2 pts The values of (double) 5 / 2 and (double) (5 / 2) are identical. Group of answer choices True False Question 2 2 pts
Question 1 2 pts
The values of (double) 5 / 2 and (double) (5 / 2) are identical.
Group of answer choices
- True
- False
Question 2 2 pts
The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as
_______________
Question 3 6 pts
Given the nested if-else structure below, answer questions below.
If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? _______________
If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed? _______________
If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed? _______________
Question 4 2 pts
Suppose we want to condition an if statement on whether two String objects, referenced by strOne and strTwo, are the same. Which of the following is the correct way to achieve this?
Group of answer choices
- if(strOne.compareTo(strTwo))
- if(strOne.equals(strTwo)) or if(strOne.compareTo(strTwo))
- if(strOne.equals(strTwo))
- if(strOne == strTwo) or if(strOne.equals(strTwo))
Question 5 2 pts
Consider the following code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score.
if (score >= 90) grade = ‘A’;
if (score >= 80) grade = ‘B’;
if (score >= 70) grade = ‘C’;
if (score >= 60) grade = ‘D’;
else grade = ‘F’;
Group of answer choices
- This code will work correctly in all cases
- This code will work correctly only if grade < 60
- All of the possible answers
- This code will work correctly only if grade < 70
- This code will work correctly only if grade >= 60
Question 6 2 pts
Which of the following is a legal way to declare and instantiate an array of 10 Strings?
Group of answer choices
- String s = new String(10);String[ ] s = new String;
- String[ ] s = new String[10];
- String s = new String[10];
- String[10] s = new String;
Question 7 2 pts Java代考
What is wrong with the following assignment statement? Assume strOne and strTwo are both String objects.
String strOneTwo = strOne.equals(strTwo);
_______________
Question 8 4 pts
Assume that aa = “11”, bb = “22”, m = 5 and n = 7. How do the following two statements differ?
System.out.println(aa + bb + m + n);
System.out.println(m + n + aa + bb);
Explain your answers in details. Only clear explanations will be given marks.
Question 9 5 pts
Write a code fragment that prints the characters stored in a String object called word backwards.
For example, if word = “Java” then the output will be “avaJ”
Question 10 6 pts
What is output by the following program?
Output 1: _______________
Output 2: _______________
Output3: _______________
Question 11 4 pts Java代考
Complete the following class by filling in multiple blanks:
public class Testing { public static void main(String[] args) { int i = 4; int j = 5; // Fill in the code to invoke the summation method and display the sum of i and j. _______________; } public static int summation(int i, int j) { // Fill in the code here to return the sum of i and j. _______________; } }
Question 12 4 pts
Complete the following class by filling in multiple blanks:
public class Testing { public static void main(String[] args) { int i = 4; int j = 5; // Fill in the code to invoke the printSum method to display the sum of i and j. _______________; } public static void printSum(int i, int j) { // Fill in the code here to display the sum of i and j. _______________; } }
Question 13 4 pts Java代考
Transform the following while loop into an equivalent for loop (make sure it produces the same output).
Question 14 8 pts
Fill in the blanks of the following code to read some data and output a greeting message displaying the attributes of the greeted person.
import java.util.Scanner; public class Testing { public static void main (String [] args) { Scanner reader = new Scanner(System.in); String name; int age; double weight; // get the data from user System.out.print ("Enter your name (a string) which will be saved in variable called name: "); _______________; System.out.print ("Enter your age (an integer) which will be saved in variable called age: "); _______________; System.out.print ("Enter you weight (a double) which will be saved in variable called weight: "); _______________; // print a greeting to name while specifying his/her age and weight _______________); } }
Question 15 2 pts Java代考
When comparing any primitive type of variable, == should always be used to test to see if two values are equal.
Group of answer choices
- True
- False
Question 16 5 pts
Write a while loop that verifies that the user enters an even integer value.
Question 17 5 pts
Write a code fragment that determines and prints the number of times the character ‘a’ appears in a String object called name.
Hint: You can use either a while loop OR a for loop
Question 18 4 pts
Describe what problem occurs in the following code. What modifications should be made to it to eliminate the problem?
Question 19 6 pts
Write code that sets each element of a Boolean array called flags to alternating values (true at index 0, false at index 1, true at index 2, false at index 3, etc.).