CS1 H.W. 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

An expression adds a byte variable and a short variable. Of what data type will the result be?

Short

high to low priority

() - */% +-

right to left

- unary negation

multiline comment

/* */

documentation comments

/** */

ignored by compiler

//

True or False: b. If one of an operator's operands is a double and the other operand is an int, Java will automatically convert the value of the double to an int.

False

scanner class

Provides convenient methods for reading input values of various types. import java.util.Scanner;

True or False: a. A variable must be declared before it can be used.

True; Variables must be declared before they can be used. A variable declaration tells the compiler the variable's name and the type of data it will hold.

Double quote escape sequence

\"

single quote escape sequence

\'

Backslash escape sequence

\\

backspace escape sequence

\b

new line escape sequence

\n

return escape sequence

\r

Horizontal tab escape sequence

\t

starting out

public class ________; public static void main(String[] args); { }

Assume that a program uses the named constant pi to represent the value 3.14. The program uses the named constant in several statements. What is the advantage of using the named constant instead of the actual value 3.14 in each statement?

A named constant is a variable whose value is read only and cannot be changed during the program's execution.

Describe in your own words what a named constant is. What is the difference between a variable and a literal and a named constant? Why is it a good idea to use named constants rather than literals in your programs?

The final key word can be used in a variable declaration to make the variable a named constant. Named constants are initialized with a value, and that value cannot change during the execution of the program. A variable is a named storage location in the computer's memory. A literal is a value that is written into the code of a program. A literal is a value written in the code. - A named constant is a variable who's value cannot be changed in the program. - It is a good idea so that you will receive an error if it ever changes.

Which type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place?

Cast

left to right

* / % + -

What will the following code segments print on the screen? int a, x = 23; a = x % 2; System.out.println(x + "\n" + a);

23 1

What does a variable declaration tell the Java compiler about a variable?

A variable declaration tells the compiler the variable's name and the type of data it will hold, e.g. int value; This line indicates the variable's name is value. The word int stands for integer, so value will only be used to hold integer numbers.

What do we mean by number wrap-around? Give an example of when this happens!

The whole-number types in Java (e.g. int, long, etc.) have limited ranges. Of course, any type representable in a finite computer has a limited range, but you're likely to actually run into the limits of the Java primitive types before you have to deal with a number that you can't represent on a computer.Manipulating a variable so that its value would exceed the range of its type results in integer overflow. When this happens, the value of the variable "wraps around"to the opposite end of the range, just as a classic video game character might wrap around to the other side of the screen upon crossing an edge.

8 Java primitive data types

byte 1byte short 2 int 4 long 8 float 4 double 8

When Java converts a lower-ranked value to a higher-ranked type, it is called a

c. widening conversion; In assignment statements where values of lower-ranked data types are stored in variables of higher-ranked data types, Java automatically converts the lower-ranked value to the higher-ranked type. This is called a widening conversion.

highest to lowest rank

double float long int short byte

As expression adds a double variable and a long variable. Of what data type will the result be?

double?

Which keyword is used to declare a named constant?

final

briefly describe the difference between variable assignment and variable initialization.

initialization: to initialize a variable. It can be done at the time of declaration.assignment: to assign value to a variable. It can be done anywhere, only once with the final-identifier.declaration: to declare value to a variable.


Ensembles d'études connexes

chapter 12 Sexual Variants, Abuse, and Dysfunctions

View Set

Module 07- sensation and the senses

View Set

BIO182 flowers and fruits amplifire

View Set

Chapter 10 - Pay for Performance

View Set

Psychology 103: Chapter 7- Thinking, Language, and Intelligence

View Set

Chapter 8:axial and appendicular skeleton

View Set

Chapter 55: Real estate purchase options

View Set

CH: 30 Malignant Hematologic Disorder

View Set

Corticosteroids Review Questions

View Set

Calculating Break Even Points and sale price

View Set