CIT: 332 Systems Development II @ WKU - Spring 2019 Study Tool

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following are valid identifiers? $_ _123 26a main a63

$_ _123 main a63 ----- this is because it cannot start with a number or a+b

Complete the following code segment to read a double value to variable radius from the keyboard. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); // your code start here // your code end here System.out.println(radius); } }

// your code start here Double radius = stdin.nextDouble(); // your code end here

Complete the following code segment to make it a working program. The program reads in a three digit integer from the keyboard and displays the sum of the digits in the integer. For example, for input 362, the program displays 11. You may assume that the inputs are always valid. You may use a sequence of integer mod % and integer divide / to get each digit. import java.util.Scanner; public class Test { public static void main(String[] args) { // your code start here // your code end here System.out.print(sum); } }

// your code start here Scanner stdin = new Scanner(System.in); int number = stdin.nextInt(); int x = number / 100; int y = (number % 100) / 10; int z = number % 10; int sum = x + y + z; // your code end here

Complete the following Java program that reads three integers, and finds the smallest value of the three integers. [Hint: use a variable to keep track of the smallest number from num1, num2 to num3.] import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num1 = input.nextInt(); int num2 = input.nextInt(); int num3 = input.nextInt(); // your code start here // your code end here System.out.println(min); } }

// your code start here int min = num1; if(num2 < min) min = num2; if(num3 < min) min = num3; // your code end here

Complete the following code segment so that the program echoes the input from the keyboard. import java.util.Scanner; public class Test { public static void main(String[] args) { // your code start here // your code end here System.out.println(stdin.nextLine()); } }

// your code start here Scanner stdin = new Scanner(System.in); // your code end here

Given variables x and y have been declared and initialized, compute the difference between them and assign the difference to a double variable diff. Code segment only, no complete program!

double diff = Math.abs(x-y);

Complete the following code segment to read an integer to variable age from the keyboard. import java.util.Scanner; public class Test { public static void main(String[] args) { int age; // your code start here // your code end here System.out.println(age); } }

// your code start here Scanner stdin = new Scanner(System.in); age = stdin.nextInt(); // your code end here

Complete the following code segment to make it a working program. The program reads a double value in gallons and displays it in liters. One gallon is 3.8 liters. import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); double gallons; // your code start here // your code end here System.out.printf("%.2f%n", liters); } }

// your code start here double liters; gallons = stdin.nextDouble(); liters = gallons * 3.8; // your code end here

Complete the following code segment to read a String value to variable name from the keyboard. // your code start here // your code end here public class Test { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String name = stdin.next(); System.out.println(name); } }

// your code start here import java.util.Scanner; // your code end here

Use println statements to print the following multiplication table: (see picture) import java.util.Scanner; public class Test { public static void main(String[] args) { // your code start here // your code end here } }

// your code start here int one = 1; int two = 2; int three = 3; System.out.println(one); System.out.println(two + "\t" + two * two); System.out.println(three + "\t" + three * two + "\t" + three * three); // your code end here

Evaluate the following expressions. 10 - (2 - 2) = 11 % 2 = 11.0 / 2 =

10 1 5.5

What's the value of tax after the execution of the following code segment: double tax = 0, price = 100, category = 3; tax = price * 0.05; if (category == 1) tax += price * 0.02; else if (category == 2) tax += price * 0.03; else if(category == 3) tax += price * 0.05;

10.0

What's the output from the following code segment? int x = 17; int y = 7; System.out.println( x= x + 1 % y); System.out.println( x++ % y); System.out.println( ++x % y); System.out.println( x-- % y); Hints: this is a sequence of statements.

18 4 6 6

What's the output of this Java code when 19 is entered? public static void main(String[] args) { Scanner input = new Scanner(System.in); int user_age = input.nextInt(); switch (user_age){ case 18: System.out.println("18"); break; case 19: System.out.println("19"); case 20: System.out.println("20"); break; default: System.out.println("Not in 18, 19, or 20"); } }

19 20

What's the value of x after the execution of the following code segment? int x = 5; x *= x++;

25

double pi = Math.PI; System.out.printf("%.3f", pi); System.out.printf("%10.2f", pi); System.out.printf("%-10.2f", pi);

3.142 3.14 3.14 ----- it was asking how it would format output.

What's the value of x after the execution of the following code segment? int x = 5; int y = 3; x *= y + 3;

30

What is the output of the following code segment? public static void main(String[] args) { System.out.println(numOfDays(12, 2000)); System.out.println(numOfDays(2, 2000)); System.out.println(numOfDays(4, 2000)); System.out.println(numOfDays(12, 2001)); System.out.println(numOfDays(2, 2001)); System.out.println(numOfDays(4, 2001)); } public static int numOfDays(int month, int year) { int days; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; case 2: if (isLeap(year)) { days = 29; } else { days = 28; } break; default: days = 0; System.out.println("Not valid input"); } return days; } public static boolean isLeap(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); }

31 29 29 31 28 28

System.out.println("55" + 5);

555

Evaluate the following expressions. 9/2*2 - 2 = 7 - 11 / 2 = 11.0/2*2 =

6 2 11.0

Give the value for the following expressions. 10 - 2 - 2 = 11 / 2 = 15 % 6 =

6 5 3

Given int variable x with value 5, match the value of y with the following statements. y = ++x; y = ++x + 1; y = x++; y = --y;

6 7 5 4

What's the output from the following code segments? int x = 7; int y = 2; System.out.println(x + y / y); System.out.println(x + y - y); System.out.println( x % y); System.out.println( y % x);

8 7 1 2

What's the output from the following code segments? int x = 7; int y = 2; double z = 2.0; System.out.println(x + y); System.out.println(x- y - z); System.out.println( x / y); System.out.println( x / z);

9 3.0 3 3.5

In Java, the "greater than or equal to" operator is __________ , and "not equal" operator is __________.

>= !=

A String variable letter1 is initialized to "a", and a char variable letter2 is initialized as 'a'. Is letter1 equal to letter2?

No

Is String value "5" equal to int value 5?

No

Which of the following statements about Identifiers are true? There is more than one correct statement. Check all the true statements. No space character is allowed in an identifier. Each identifier starts with a lower case letter. Identifiers must start with a letter, underscore(_), or dollar sign($). They are case sensitive.

No space character is allowed in an identifier. Identifiers must start with a letter, underscore(_), or dollar sign($). They are case sensitive.

Provide an example of literals used in Java.

Numeric literals: constant integer or floating point numbers. Character literals: a single character enclosed in single quotes, Boolean literals: true or false. String literals: a sequence of characters enclosed in double quotes. ----- Example: 5 represents a constant int with value 5. 5.3 represents a constant double with value 5.3. 'A' represents a constant char with value character A. true and false are two and only two boolean literals, which represent true and false for a boolean value. "ABC" represents a constant String which contains three characters A, B, and C.

Write a Java program to determine a student's grade. The program can read four scores in double value type and assign the grade to a string variable letterGrade based on the following rules: if the average score >= 90, then A if the average score >= 80 and < 90, then B if the average score >= 70 and < 80, then C if the average score >= 60 and < 70, then D if the average score < 60, then F import java.util.Scanner; public class Test { public static void main(String[] args) { // your code start here // your code end here System.out.println(letterGrade); } }

Scanner stdin = new Scanner(System.in); double score1 = stdin.nextDouble(); double score2 = stdin.nextDouble(); double score3 = stdin.nextDouble(); double score4 = stdin.nextDouble(); double grade = (score1 + score2 + score3 + score4) / 4; String letterGrade; if (grade >= 90) letterGrade = "A"; else if (grade >= 80) letterGrade = "B"; else if (grade >= 70) letterGrade = "C"; else if (grade >= 60) letterGrade = "D"; else letterGrade = "F";

Given a double variable x which has been declared and initialized, use printf to print out the value of x with two digits after the decimal point. Code segment only, no complete program!

System.out.printf("%.2f", x);

Given a String variable name, use printf to print out the value of name with 16 characters with blanks padded to the left. Code segment only, no complete program!

System.out.printf("%16s", name);

true, false are ___________ in Java.

boolean literals

Given two double variables x and y, which have been declared and initialized, compute the geometric mean and store it to geoMean(has not been declared). Code segment only, no complete program!

double geoMean = Math.sqrt(x * y);

Declare a double variable gpa, and initialize it with 3.5.

double gpa = 3.5;

Declare a double variable rootOf3 and assign the square root of 3 to it. Code segment only, no complete program!

double rootOf3 = Math.sqrt(3);

Declare three double variables x, y, and z, and initialize them with 3.0, 4.5, 9.9 respectively.

double x = 3.0; double y = 4.5; double z = 9.9;

There are four integral built-in primitive types in Java, these types are __________, __________, __________, __________.

byte short int long

Which keyword may be used in a switch statement? case default switch break

case default switch break

Declare a char variable ch and initialize it with 'Z'.

char ch = 'Z';

Declare a char variable grade, and initialize it with uppercase A.

char grade = 'A'; ----- when not using ' but instead " it then becomes a string -- char uses ' and strings use "

What's the output of the following code segment? boolean check = false; if (check = true) System.out.println("check is true"); if(check = false) System.out.println("check is false");

check is true

Given the coordinates of two points x1, y1, x2, and y2(all declared and initialized) compute the distance between the two points and store the results to variable distance(declared). Code segment only, no complete program!

distance = Math.hypot(x1-x2, y1-y2);

Given two double variables, x and y, which have been declared and initialized. Declare a double variable avg and assign the average of x and y to avg. Code segment only, no complete program!

double avg; avg = (x + y) / 2; ----- x and y have already been typed thus why we only do avg. the hint that it is already typed is "declared and initialized". we declare avg with double avg; then it wants us to assign the math to find the average (mean) to that variable thus why we did avg = (x+y) / 2;

Given that a double variable x has been declared and initialized, declare a double variable y and assign y with the value computed by the following formula: y =3 x^3 -2*x +7

double y; y = 3 * x * x * x - 2 * x + 7;

What's the value of the following expression: 55 / 10 * 10 == 55 true false cannot be determined not a valid expression 55

false -- PIMDAS answer=0.55 not 55

Give an int variable num which has been declared and initialized, determine whether or not num is a multiple of 5. If num is a multiple of 5 print "yes", otherwise print "no". Use if-else to solve the problem. Use System.out.println to do the print. Code segment only, no complete program!

if (num % 5 == 0){ System.out.println("yes"); } else System.out.println("no"); -- if input is a multiple of 5 it is yes and if not then it is no. 5,10,15(etc) is yes and everything else is no. % is remainder

Rewrite the following statement with conditional operators to an equivalent if-else statement. x and y have been declared and initialized, and abs has been declared. abs = x > y ? x - y : y - x;

if (x > y) abs = x - y; else abs = y - x;

Given both m and n have been declared and initialized, and max has been declared, which if-else statement is invalid to assign the max m and n to variable max? if (m > n) max = m; else max = n; if (m > n) { max = m; } else { max = n; } if (m > n) max = m; else { max = n; } if m > n max = m; else max = n;

if m > n max = m; else max = n;

Given an int variable num which has been declared and initialized, determine whether or not num is a perfect square(like 1, 4, 9, 16 ....). If num is a perfect square print "perfect", otherwise print "not". Use System.out.println to do the print. Code segment only, no complete program!

if(Math.round(Math.sqrt(num))==Math.sqrt(num)){ System.out.println("perfect"); } else System.out.println("not");

Given a double variable speed which has been declared and initialized. Print(use println) "fast" if the speed is greater than 70, print "good" otherwise. You don't have to use else to solve this problem. Code segment only, no complete program!

if(speed > 70) System.out.println("fast"); if(speed <= 70) System.out.println("good");

Given a double variable temperature which has been declared and initialized. Print(use println) "hot" if the temperature is greater than 90. Code segment only, no complete program!

if(temperature > 90) System.out.println("hot"); -- test with the following input: 100 120 90 80 91

Given a double variable temperature which has been declared and initialized. Print(use println) "hot" if the temperature is greater than 90, otherwise, print "not hot". You don't need to use if-else to solve this problem. Code segment only, no complete program!

if(temperature > 90) System.out.println("hot"); if(temperature <= 90) System.out.println("not hot");

Based on the value of the variable temperature, assign the state of water to variable state. The variable temperature has been declared and initialized, and the String variable state has been declared. The state of water can be determined based on the following table: temperature ≤ 32 solid 32 < temperature < 212 liquid temperature ≥ 212 gas Code segment only, no complete program!

if(temperature >= 212) state = "gas"; else if (temperature > 32) state = "liquid"; else state = "solid";

Declare two int variables length and width. Initialize length to 10, and width to 15.

int length = 10; int width = 15;

Declare an int variable million and assign 2^20 to it. Code segment only, no complete program!

int million = (int) Math.round(Math.pow(2,20)); ----- math.round is converts double to integer while math.pow assigns the exponent as a double.

Declare an int variable num and initialize it with the largest five digits decimal integer.

int num = 99999;

Rewrite the following if statement using the ternary operator. if(x > (x + y) / 2) z = x; else z = y; (see pic for code it is inserted into)

int z = x > (x + y) / 2 ? x : y;

What are the eight primitive data types in Java for reserved variable words?

integral types: byte short int long floating-point types: float double boolean, the type whose values are either true or false. char, the character type whose values are 16-bit Unicode characters.

For an integer variable num which has been declared and initialized, determine if it is a multiple of 6. If yes, assign true to boolean variable isMultiple(already declared), otherwise, assign false to isMultiple. The answer is one single statement. Code segment only, no complete program!

isMultiple = num % 6 == 0 ? true : false;

The execution of a Java program starts with the __________ method.

main

What's the output of the following code segments? int num = 6; if (num % 3 == 0) System.out.println("multiple of 3"); else if (num % 2 == 0) System.out.println("multiple of 2"); else System.out.println("NOT multiple of 4");

multiple of 3

What's the output of the following code segments? int num = 6; if (num % 3 == 0) System.out.println("multiple of 3"); if (num % 4 == 0) System.out.println("multiple of 4"); else System.out.println("NOT multiple of 4");

multiple of 3 NOT multiple of 4

Which of the following are valid methods in Scanner class? Check all that apply. nextLong() next() nextline() nextDouble() nextInt()

nextLong() next() nextDouble() nextInt() ----- case sensitive that is why nextline() is not one because it must be nextLine() to be a method in Scanner class.

The header of the main method starts with three keywords, they are __________, __________, __________.

public static void

Which of the following statements are true? There is at least one correct statement. Check all the true statements. public is a keyword println is a method. The file name for the public class must match with the class name. stdin is a variable.

public is a keyword println is a method. The file name for the public class must match with the class name. stdin is a variable.

What's true about the following code segment? int num = 5; String result; if(num > 10) { if(num < 10) result = "small"; else result = "some where" } System.out.println(result); -- cannot determine the output runs fine, produces no output result in a compile error print "some where" on the screen

result in a compile error -- local variables not declared properly for string and missing a semicolon after "some where"

What's true about the following code segment? int num = 5; String result; if(num > 10) result = "big"; if(num < 10) result = "small"; System.out.println(result); -- runs fine, produces no output result in a compile error print "small" on the screen cannot determine the output

result in a compile error -- local variables not declared properly for string.

Most of Java statements are ended with the punctuation:

semicolon

Convert the following if-else statement to an equivalent if statement(no else) by giving the correct condition. if (size> 100) System.out.println("big"); else System.out.println("small"); (see pic for code it is inserted into)

size <= 100 -- check picture to understand why it is like this.

How must an identifier start in order to be considered an identifier?

start with a letter, underscore(_) or dollar sign($), and followed by 0 or more letters, digits, underscore or dollar sign. ----- example, abc, $1, _func are all valid identifiers, while 1abc, a+b are not valid identifiers

Given a int variable grade which has been declared and initialized, determine the letter grade and assign it to a string variable letterGrade(declared). In your solution, you should not use any if or if-else statement. consider use int division(/) operator. Code segment only, no complete program!

switch(grade / 10) { case 10: case 9: letterGrade = "A"; break; case 8: letterGrade = "B"; break; case 7: letterGrade = "C"; break; case 6: letterGrade = "D"; break; default: letterGrade = "F"; }

What's the value of the following expressions: true || false true && false true && !false

true false true

Which of the following expression determines whether or not x is a valid value for the following function (see picture for problem) x != 3 && x => 2 && x =< -2 x != 3 || (x > 2 || x < -2) x != 3 && (x >= 2 || x <= -2) x >3 || x <= -2 -- problem in picture read as: square root of x^2 - 4 / x -3

x != 3 && (x >= 2 || x <= -2)

Given that x and y are two int variables that have been declared and initialized, which of the following statements will result in a syntax error? x /= y; x + = y; x += x; x == y;

x + = y; x == y;

Given int variable has been declared and initialized, Which of the following statements will result in a syntax error? x = x++; x = ++x+; x = +x++; ++x++;

x = ++x+; ++x++;

Give the conditional expression to determine the max of two variables x and y. x and y have been declared and initialized. Code segment only, no complete program!

x > y ? x : y


Conjuntos de estudio relacionados

History- Political Statement: from the Universal Declaration of Human Rights

View Set

Econ 1101 - Final Exam Review (Compilation of all Chapter Exam Reviews)

View Set

Sociology Chapter 2: Sociological Research Methods

View Set

Exam 2 Material for Human Injuries and Preventative Medicine

View Set

MGMT 320 EXAM2 CH 11 question bank

View Set

Texas Life and Health: Life Policy Riders, provisions, Options and Exclusions

View Set

Mental Health: Chapter 8: Assessment

View Set

NU273 Chapter 24: Cognitive Disorders

View Set