Java Computer Science Chapter 4 Test

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

the ____ operator computes the remainder of a division

%

The ___ operator increments a variable

++

The ______ operator decrements a variable

--

what is the output Math.round(-4.49);

-4

string positions are counted starting with ____

0

7%4

1

what is wrong with the following statement? double circumference = 3.14*diameter

1. you should use a named constant, not the 'magic number' 3.14. 2. 3.14 is not an accurate representation of pi

what is the output Math.round(13.15);

13

what is the output double balance = 13.75; int dollars= (int) balance;

13

what is the output Math.round(13.75);

14

what is the output double balance = 15.9; int dollars= (int) balance;

15

what is the output int dollars= (int) 15.9; double balance = dollars;

15 when using the int cast it changes 15.9 to 15 and stores it as 15.

what is the output Math.round(15.95);

16

What is the value of 1729/100? 1729%100?

17 and 29

what is 7/4

2

what is 7/4.0

2.5

of the 6 primitive types that are numbers, _____ of them are for integers and ___ for floating-point numbers.

4,2

_______ of the primitive types are number types.

6

How many primitive data types are there in Java?

8

Which is an example of a class that takes user input CashRegisterSimulator CashRegisterTester

CashRegisterSimulator

What is the output System.out.println("Cube root of 1090: "+Math.cbrt(1090));

Cube root of 1090: 10.291424665715065

What is the output System.out.println("Cube root of 10: "+Math.cbrt(10));

Cube root of 10: 2.154434690031884

What is the output System.out.println("Cube root of 625: "+Math.cbrt(625));

Cube root of 625: 8.549879733383484

How do you convert a floating-point value to an int with a cast?

EXAMPLE double balance = 13.75; int dollars= (int) balance; this makes the balance an int before it stores it

what is the output String greeting= "Hello, World!"; String sub= greeting.substring(0,5)

Hell

if a string contains the digits of a number, you use the ___________ or ___________ method to obtain the number value

Integer.parseInt, Double.parseDouble

Use the __________ method to round a floating-point number to the nearest integer

Math.round

IN REAL LIFE, can you use floating-point numbers in financial programs

NO, this is due to the very real possibility of a conversion/rounding error. professional programs use the BigDecimal type for this purpose (see advanced topic 4.1 for help) (if you do not know why floating-points have issues rounding: When a value cant be converted exactly, it is rounded. this can occur when converting between binary and decimal numbers, or integers and floating-point numbers. For example double f= 4,.35 syso(100 * f); //prints 434.999999994 Why? because computers represent number in the binary number system. in this system, there is no exact representation of fractions like 1/10 and 1/3.)

Use the _____ class to read keyboard input in a console window

Scanner

What is char

The character type, representing code units in the Unicode encoding scheme

Why are rounding errors are a very serious issues with floating-point numbers

When a value cant be converted exactly, it is rounded. this can occur when converting between binary and decimal numbers, or integers and floating-point numbers. For example double f= 4,.35 syso(100 * f); //prints 434.999999994 Why? because computers represent number in the binary number system. in this system, there is no exact representation of fractions like 1/10 and 1/3.

What is a numerical constant

a numerical value that does not change

What is the output double a = -23.45; int b = 234; double c = 12.54; System.out.println("absolute value of a: "+Math.abs(a)); System.out.println("absolute value of b: "+Math.abs(b)); System.out.println("absolute value of c: "+Math.abs(c));

absolute value of a: 23.45 absolute value of b: 234 absolute value of c: 12.54

the = operator is called the _________ __________

assignment operator

How do you round the double value x to the nearest value, assuming that you know that it is less than 2*10^9

by using a cast: (int) Math.round(x)

The primitive integer type _____ has a size of 1 byte

byte

the primitive number type ____ has a range of -128 to 127

byte

put the following numerical primitive types in order from smallest to greatest range: int, byte, long, short

byte, short, int, long

you can use a _____ (typeName) to convert a value to a different type.

cast

What are the two primitive types that are not used for number

char and boolean

The primitive floating point type _____ has a size of 8 bytes

double

the primitive number type ____ has a range of about negative to positive 10^308

double

which floating point primitive type has a greater range: float or double

double

what is the output double balance = 15.9; int dollars= balance;

error you can not store a double in an int

True or false The BigDecimal type still has roundoff errors in floating-point computation

false

True or false? assignment to a variable is the same as mathematical equality

false

True or false? Strings can be concatenated (put end to end to yield a new, longer string. String concatenation is denoted by the * operator

false although the first part is true, string concatenation is denoted by the + operator not the * operator

true or false the assignment operator, =, indicates that the object to the left and right of it are equal

false it is an instruction to copy the right hand side variable into the left hand side variable

True or false? it is legal to assign a floating-point variable to an integer value, but the other way around is illegal

false you can assign an int value to a double value though. to overcome this problem you can convert the floating-point value to an int with a cast.

True or false unlike strings, numbers are objects

false, strings are objects and numbers are not

true or false java warns you when an integer overflow occurs

false, it will compute a completely wrong value and not tell you

true or false when dealing with the BigInteger and BigDecimal classes in the java.math package, you can use familiar operators such as +, -, *

false, you must use methods called add, subtract, and multiply. ex BigInteger a= new BigInteger("1234567890"); BigInteger b= new BigInteger("9876543210"); BigInteger c= a.multiply(b); Syso(c); //prints 12193263111263526900

a ______ variable is a constant. once it's value had been set, it cannot be changed.

final

The primitive floating point type _____ has a size of 4 bytes

float

the primitive number type ____ has a range of about negative to positive 10^38

float

rounding errors are a very serious issues with ________________ numbers

floating-point

The primitive integer type _____ has a size of 4 bytes

int

the primitive number type ____ has a range of about negative to positive 2 billion

int

which are the most commonly used number types in java

int and double

If both arguments of the / are integers, the result is an _______

integer

What does a cast do?

it converts a value to a different type ex double balance = 13.75; int dollars= (int) balance; this makes the balance an int before it stores it

The primitive integer type _____ has a size of 8 bytes

long

the primitive number type ____ has a range of about negative to positive 9 quintillion (+-9,223,372,036,854,775,808)

long

use _______ constants to make your programs easier to read an maintain

named

is string a primitive type

no

is the call syso(4) a static method call?

no-- the println method is called on the object System.out

What is the value of n after the following sequence of statements? n--; n++; n--:

one less than it was before

Why doesnt the following statement compute the average of s1, 2, and s3? double average= s1+s2+s3/3;

only s3 is divided by 3, to get the correct result, use parentheses. moreover, if s1, s2, and s3 are integers, you must divide by 3.0 to avoid integer division: (s1+s2+s3)/3.0

a numeric computation ______ if the result falls outside the range for the number type

overflows

_______ errors occur when an exact conversion between numbers is not possible

rounding

assuming the String variable holds the value "Agent", what is the effect of the assignment s= s+s.length()

s is set to the string Agent5 s=Agent+Agent.length() s=Agent5

The primitive integer type _____ has a size of 2 bytes

short

the primitive number type ____ has a range of -32768 to 32767

short

a ________- method does not operate on an object

static

a ________ is a sequence or characters

string

use the _______ method to extract a part of a string

substring

why cant input be read directly from System.in?

the class only has a method to read a single byte. it would be very tedious to form characters, strings, and numbers from those bytes.

what is the difference between the following two statements? final double CM_PER_INCH = 2.54; and public static final double CM_PER_INCH

the first definition is used inside a method, the second inside a class

what is boolean

the primitive type with two truth values: false and true

How does java recognize when a programmer programs char

the programmer puts single quotations around the character. ie: 'h'

what is the value of Math.sqrt(Math.pow(x,2)+Math.pow(y,2)); in mathematical notation?

the sqrt of (x^2+y^2)

what is the meaning of the following statement? balance= balance+amount;

the statement adds the amount value to the balance variable.

assuming the String variable river holds the value "Mississippi", what is the value of river.substring(1,2)? of river.substring(2, river.length()-3)?

the strings "i" and "ssissi"

Suppose in is a Scanner object that reads from System.in, and your program calls String name= in.next(); what is the value of name if the user enters John Q. Public?

the value is "John". the next method reads the next word.

What is one reason that a programmer will use the primitive type short instead of int

to save storage

true or false good coding style says that constants should be capitalized

true

true or false whenever one of the arguments of the + operator is a string, the other argument is converted to a string

true

In the unlikely event that the long type fails, what can a programmer do?

us the arbitrary-precision BigInteger type. (See advanced topic 4.1 for help)

on the left of an assignment operator, you need a ________ _________. on the right side it can be a _________ or an _________.

variable name, single value, expression

When does the cast (long) x yield a different result from the call Math.round(x)

when the fractional part of x is greater or equal to 0.5

why cant you call x.pow(y) to compute x^y

x is a number not an object. you cannot invoke methods on numbers


Set pelajaran terkait

CISI derivatives: End of Chapter questions

View Set

Prep U Chapter 28 Med/Surg Heart Failure

View Set

Preguntas y Respuestas con Nombres

View Set

PS1: Opportunity cost & Production Possibility Frontier

View Set

Prioritization, Delegation, Assignment-Chapter 12

View Set

Semester 2 Physical Science Study Guide

View Set