CSE 174 Exam Review

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What value will be printed by this code: int x = (1/3 + 1/3 + 1/3); System.out.println(x);

0

Which of the following Java expressions will have a value of 2.5? 5 / 2 1.0 / 2.0 + 2 3.0 - (1 / 2) 5 / 2.0 5.0 / 2.0 2 + 1 / 2

1.0 / 2.0 + 2 5 / 2.0 5.0 / 2.0

To keep your code more readable, you should break up long lines of code into shorter lines. In CSE 174, what is the recommended maximum line length? 80 characters 40 characters 60 characters 120 characters

80

Which of the following is the required tool for converting Java source code into Java byte code?

A java compiler

Which of the following is required in order to run Java byte code?

A java virtual machine

Which of the following are true about this printf() statement? Assume that the variable age is an int and has been declared and assigned a value. System.out.printf("%-5d", age); If age is 47, it will be padded with 3 spaces The - will cause age to be printed as a negative number as a negative number The number will appear "left-aligned". If age is 123456, then one of the digits will not be displayed

If age is 47, it will be padded with 3 spaces The number will appear "left-aligned".

If a Java program contains multiple methods, how does the Java virtual machine know which method should be executed first?

It will execute the method named main.

In Java, there is an upper limit to the size of an int. T/F

T

What is the recommended way to indent blocks of code? With 5 spaces With 4 spaces Each programmer needs to decide what personally looks better With a Tab character

With 4 spaces

According to the Java API documentation for the PrintWriter class (version 15), which of the following methods exist? close() flush() print() clear() printf() println()

close() flush() print() printf() println()

Classify each error as either a compile-time error or a run-time error: missing an open curly brace adding an extra closing brace multiplying 3 numbers together when you should have added them together instead program throws an exception because you divided by zero run-time forgetting to put a semicolon at the end of a line

compile-time compile-time run-time run-time compile-time

The missing quotation marks in the following line of code will result in what kind of error? System.out.println(Hi there!); run-time error logic error exception compile-time error

compile-time error

According to the Java API documentation for the File class (version 15), which of the following methods exist? delete() exists() getFolder() getName()

delete() exists() getName()

Which of the following statements will cause a compiler error? double a = -45; int e = 5.0; float c = 2.9f; char f = 'cat'; double d = 8.0f; float b = 3.5;

int e = 5.0; char f = 'cat'; float b = 3.5;

Which of the following lines of code declare a variable? height = 6; int height = 6; height = height + 5; int height;

int height = 6; int height;

Java's Scanner class is part of which package? java.Scanner java.lang java.util java.io

java.util

Match each Java class with the package in which the class is contained. Scanner File PrintWriter FileNotFoundException

java.util java.io java.io java.io

Which Scanner method should be used in order to read a String from the keyboard? nextChar() nextString() next() nextInt()

next()

Which of the following names would be best for a variable that holds the number of people in a person's family? NUMBER_OF_PEOPLE_IN_FAMILY np n numberOfPeopleInFamily NumberOfPeopleInFamily

numberOfPeopleInFamily

In java, an exception is an example of a:

run-time error

What number will be printed by each each of the following println() statements? int width = 5; int height = 8;int area = width * height; System.out.println(area); // width = 10; height = 3; System.out.println(area); //

40 40

What value is stored in x: int x = 47 / 2 * 2;

46

What value will be printed by this code: double x = 5325; double result = x/100; System.out.println(result);

53.25

What is the value of this Java expression? 53 + 7 / 2

56

What will be the value stored in result: int x = 5771; int result = x % 100;

71

What value will be printed by this code: int x = 72; x++; System.out.println(x);

73

What is wrong with this code: int bodyTemperature = 98.6; System.out.println(bodyTemperature); 98.6 is not a valid value for the int data type, so the value will be rounded. 98.6 is not a valid value for the int data type, so the program will not compile. Nobody would ever want their body temperature printed. 98.6 is not a valid value for the int data type, so the program will run but then give an error message.

98.6 is not a valid value for the int data type, so the program will not compile

In jGRASP, what is the keyboard combination that will fix your code's indentation? Ctrl-T or Command-T Ctrl-F or Command-F Ctrl-Alt-Delete F2, followed by Shift-F2 Alt-Ctrl-Shift-Command-%

F2, followed by Shift-F2

The float data type is able to store more values than the double data type. T/F

False

Which of the following assigns a value to the variable height? (You may assume that all variables have already been declared and assigned values.) height = height + 1; height * 2; height = width; base1 + base2 = height; width = height; height = height + height * height;

height = height + 1; height = width; height = height + height * height;

What will be the output? public class MethodDemo { public static void main(String[] args) { System.out.println("hello"); } public static void animals() { System.out.println("dog"); System.out.println("cat"); } public static void names() { System.out.println("amy"); System.out.println("tom"); } } // end class

hello

What will be the output of the following Java program? public class MethodDemo { public static void main(String[] args) { System.out.println("hello"); names(); } public static void animals() { System.out.println("dog"); System.out.println("cat"); } public static void names() { System.out.println("amy"); System.out.println("tom"); } } // end class

hello amy tom

Which of the following are examples of names that follow appropriate style guidelines? double gpa; public static void greetTheUser() { } public class helloWorld { } int numberofdays = 13; final double TAX_RATE = 0.17;

double gpa; public static void greetTheUser() { } final double TAX_RATE = 0.17;

Which of the following are true about this printf() statement? Assume that the variable gpa is a double and has been declared and assigned a value. System.out.printf("%.4f%n", gpa); If the value in gpa is 2.112345, it will be displayed as 2.1123 If the value in gpa is 3.4, it will be displayed as 3.4000 %n can be interpreted as a linefeeed the statement is an error because it contains a comma outside the parentheses

f the value in gpa is 2.112345, it will be displayed as 2.1123 If the value in gpa is 3.4, it will be displayed as 3.4000 %n can be interpreted as a linefeeed

In the program below, how many times will "amy" be printed? public class MethodDemo { public static void main(String[] args) { System.out.println("amy"); animals(); names(); animals(); } public static void animals() { System.out.println("dog"); System.out.println("cat"); names(); } public static void names() { System.out.println("amy"); System.out.println("tom"); } } // end class

4

In each of the following statements, what (if anything) will be stored in the variable x? int x = 3 / 4; 0 int x = 3.0 / 4.0; compiler error double x = 3 / 4; 0.0 double x = (double) ( 3 / 4); 0.0 double x = 3 / 4.0; 0.75 double x = (double) 3 / 4; 0.75 double x = 3.0 / 4.0;

0 compiler error 0.0 0.0 0.75 0.75 0.75

What will be the value stored in x: int x = 471329153 % 2;

1

What will be the value stored in x: int x = 10 % 73;

10

In Java, which of the following Java expressions will give results that are same as the expected mathematical result? 12/3 10.0/4.0 2147483647 + 1 2147483646 + 1 .1 + .2 10/4 1000000000 + 2000000000 .1 + .1

12/3 10.0/4.0 214783646 +1 .1+.1

What is the value of this expression? (int) 19.95

19

Suppose you have a text file named "info.dat", and the content of the text file is as shown. 15 25 5 1 88 What number would be printed by the following code snippet? Scanner in = new Scanner(new File("info.dat")); int sum = 0; sum += in.nextInt(); in.nextInt(); sum += in.nextInt(); in.close(); System.out.println(sum);

20

What value will be printed by this code: int x = 2635; int result = x/100; System.out.println(result);

26

Which of the following variable names would cause a compiler error? 321blastoff DaysUntilVacation thisIsValid! _daysUntilVacation base1 numberOfStudents NUMBER_OF_STUDENTS double thisIsNotValid single public

321blastoff thisIsValid! double public

Which of these best describes the structure of a Java program?

A class that contains multiple methods

Where should you put blank lines in your Java programs? After the final closing curly brace at the end of the class At the start and end of each method Between sections of related code, in order to break the code up into related "paragraphs" Before and after every line of code, so that the code appears "double-spaced"

At the start and end of each method Between sections of related code, in order to break the code up into related "paragraphs"

Where are appropriate places for comments in your Java programs? Before any section of code that readers might not understand After the closing curly brace in a long method, to make it clear where that method ends Before each helper method to explain what the method does At the top of your code, to explain what the program does

Before any section of code that readers might not understand After the closing curly brace in a long method, to make it clear where that method ends Before each helper method to explain what the method does At the top of your code, to explain what the program does

Consider the following Java program. // A program that talks like a dog. // Author: Maria Gonzales // Date: May 17, 2014 public class Dog { public static void main(String[] args) { System.out.println("Woof!"); } } What should be the name of the source code file that contains this code? After compiling the above source code, what will be the name of the byte code file that is produced the compiler?

Dog.java Dog.class

Suppose you have the following code snippet. Which of the statements below are true? PrintWriter write = new PrintWriter(new File("grades.csv")); write.println("70,80,90"); write.println("98,99,100"); write.close(); If you open this file in a text editor, the data will appear on two separate lines in the file. If you open this file in a text editor, the data will appear with spaces between the numbers. If the file grades.csv already existed, the old data will remain in the file, and the new data will be added to the end. If you open this file in a spreadsheet program such as Excel, each number will appear in a separate cell, without the commas.

If you open this file in a text editor, the data will appear on two separate lines in the file. If you open this file in a spreadsheet program such as Excel, each number will appear in a separate cell, without the commas.

The Java code you type in a text editor is typically referred to as....

Java Source Code

What is the output of the following line of code? System.out.println("Roses are red,\n"); System.out.println("Violets are blue."); Roses are red, Violets are blue. Roses are red, Violets are blue. Roses are red, Violets are blue. Roses are red,Violets are blue.

Roses are red, Violets are blue.

Suppose you have a text file named "data.data", and the content of the text file is as shown. 2 3.14 -9 cat 3.7 Which of the following code snippets would be able to read from the file without any errors? Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); in.close(); Scanner in = new Scanner(new File("data.data")); in.next(); in.next(); int a = in.nextInt(); in.close(); Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); double b = in.nextDouble(); int c = in.nextInt(); String d = in.next(); double e = in.nextDouble(); double f = in.nextDouble(); in.close(); Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); int b = in.nextInt(); in.close(); Scanner in = new Scanner(new File("data.data")); String a = in.next(); String b = in.next(); String c = in.next(); String d = in.next(); String e = in.next(); in.close(); Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); double b = in.nextDouble(); int c = in.nextInt(); String d = in.next(); double e = in.nextDouble(); in.close();

Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); in.close(); Scanner in = new Scanner(new File("data.data")); in.next();in.next(); int a = in.nextInt(); in.close(); Scanner in = new Scanner(new File("data.data")); int a = in.nextInt(); double b = in.nextDouble(); int c = in.nextInt(); String d = in.next(); double e = in.nextDouble(); in.close();

What is wrong with the following Java code? double startTime = -5.3;System.out.println(StartTime); StartTime and startTime begin with different letters Negative numbers are not legal for double variables double is not a valid type of variable The code contains no errors.

StartTime and startTime begin with different letters

Which of the following are considered "safe" (widening) conversions? Storing a long value in a short variable Storing a float value in a double variable Storing an int value in a long variable Storing a double value in a long variable Storing an int value in a double variable Storing a byte value in a float variable

Storing a float value in a double variable Storing an int value in a long variable Storing an int value in a double variable Storing a byte value in a float variable

Which of the following println() statements would cause an error? System.out.println("\\"); System.out.println("\"); System.out.println("My name is "Mary""); System.out.println("/"); System.out.println("\""); System.out.println("73 / 9"); Correct! System.out.println(""");

System.out.println("\"); System.out.println("My name is "Mary"");. System.out.println(""");

Suppose you try to compile and run the following program: public class Attempt {public static void main(String[] args) { double taxRate; System.out.println(taxRate); } } The code will compile and run, and 0 will be printed . The code will not compile because taxRate has not been assigned a value. The code will not compile because taxRate has not been declared. The code will compile, but will not run because taxRate has not been assigned a value. The code will compile and run, and 0.0 will be printed.

The code will not compil because taxRate has not been assigned a value

What is wrong with this code: int base, height; int area = base * height; The result will always be zero because base and height were not initialized. You should not use the int data type for computing area. You cannot declare two variables in one line of code. The code will not compile because base and height were not initialized.

The code will not compile because base and height were not initialized

Consider the following Java program (assume all necessary import statements have been written). public class WriteStuff { public static void main(String[] args) throws FileNotFoundException { PrintWriter out = new PrintWriter(new File("mydata.data")); out.println(10); out.println(20); out.println(30); } } What will be the result of this code? The file will be created, and the data will be 10 20 30, with all values on the same line, space-separated. The file will be created, but it will not contain any data. The file will be created, and the data will be 10 20 30, with each value on a separate line. The file will not be created.

The file will be created, but it will not contain any data.

Consider the following code snippet. Which user inputs will cause an error? Scanner kb = new Scanner(System.in); int a = kb.nextInt(); double b = kb.nextDouble(); int c = kb.nextInt(); The user enters, all on one line, separated by spaces: 7 15.7 24 The user enters, all on one line: 7 15.7 24 cat dog 94 The user enters, on 2 separate lines: 715.7 24 The user enters, all on one line, separated by commas: 7, 15.7 ,24 The user enters, on 3 separate lines: 15.7 7 24

The user enters, all on one line, separated by commas: 7, 15.7, 24 The user enters, on 3 separate lines: 15.7 7 24

What is the output of the following println() statement? System.out.println("What is your \nname?"); What is your name? What is your nname? What is your nname? What is your name?

What is your name?

Which of the following will cause an InputMismatchException? You use the nextInt() method, but user enters the word cat You use the nextDouble() method, but user enters 0 You use the nextInt() method, but user enters 3.0 You use the next() method, but the user enters 10.6

You use the nextInt() method, but user enters the word cat You use the nextInt() method, but user enters 3.0

What would be the output of the following code snippet? System.out.println("a"); System.out.print("b"); System.out.println("c"); System.out.print("d"); a b c d ab cd a bc d abcd

a bc d

In a java program, which best describes the relationship between class and method?

a java program is a class that contains one or more methods

If you compile and run a program, and the program prints "hello" but you wanted it to print "goodbye", then your program contains: an exception a compile-time error a logic error

a logic error

Suppose you have the following line of code in your program. Which of the statements below are true? PrintWriter write = new PrintWriter(new File("data.txt")); If the file "data.txt" did not already exist, it will get created for you. The file "data.txt" will be saved in the same folder as the .java file for this program. If the file "data.txt" already existed, then this line of code will delete that file and create a new file. When the program has completed execution, you will be able to open the file with a text editor.

all are correct

For each of the following Java programs, will the program compile? public class Demo { public static void doSomething() { System.out.println("I'm doing something."); } } public class Demo { } public class Demo { public static void main(String[] args) { } } public class Demo { public static void main(String[] args) { System.out.println("I'm doing something."); } }

all will compile

In mathematics, the area of a triangle with a given base and height is given by the formula: a r e a = 1 2 b a s e × h e i g h t Here is the start of a Java program to compute the area of a triangle: double base = 3.0; double height = 4.0; double area = __________________________ ; 1 / 2 * base * height base * (height / 2) (1 / 2) * (base * height) 1.0 / 2.0 * base * height 0.5 * base * height base * height / 2

base * (height / 2) 1.0 / 2.0 * base * height 0.5 * base * height base * height / 2

Which of the following code snippets are valid? (Here, "valid" means "will not cause a compiler error if the code were in the body of a method". In questions like this, do not look at each individual line of code separately. For example, if a snippet has 4 lines of code in it, consider whether those 4 lines together will compile.) char firstInitial = 'G'; char lastInitial = 'H'; float f = -10; double gpa = 4.0; System.out.println(gpa); double gpa = 3.0; System.out.println(gpa); char firstName = 'Grace'; char lastName = 'Hopper'; int b = 10.0; String word = "cat"; String empty = ""; String result = word + empty; int a = 10; a = 11;

char firstInitial = 'G'; char lastInitial = 'H'; float f = -10; String word = "cat"; String empty = ""; String result = word + empty; int a = 10; a = 11;

Match each situation with the appropriate error message: You Answeredfile, but the file only contains 1 double and no other data You AnsweredTrying to read 4 doubles from a file, but the file only contains 50 doubles and no other data Trying to read an int from a file, but finding a double instead Forgetting to import the File class Forgetting to close() the Scanner that is reading from the file

runtime error: NoSuchElementException no error runtime error: InputMismatchException compiler error: Cannot find symbol file may stay "locked" preventing other programs from using it

Look up the Java API (version 15) documentation for the Scanner class. Which of the following are names of methods in the Scanner class? skip() nextBigInteger() nextShort() nextString() hasNextDouble() reverse()

skip() nextBigInteger() nextShort() hasNextDouble()

A Java IDE such as jGRASP typically includes access to which of the following tools? a text editor a video player a Java Virtual Machine a compiler a debugger

text editor JVM a compiler a debugger

If you ask the user to enter a double, and use a Scanner's nextDouble() method to read that value from the keyboard, what will happen if the user enters an int instead? the int will be converted automatically to a double the double value will be truncated to an int value this will cause a runtime error: InputMistmatchException this will cause a compiler error

the int will be converted automatically to a double


संबंधित स्टडी सेट्स

Photogrammetry Midterm Study Guide

View Set

ENG 100 Week 1,2,3,4,5/7/8/9/10/11

View Set

RCH Melb - Febrile seizure guideline

View Set