Comp Sci A Unit 1 - Primitive Types (Output in Java, User input and variables, Data types)
Consider the following code: long y = 9223372036854775807; y = y + 1; System.out.println(y); What number does the program print when compiled and run?
This program crashes and raises a longOverflow exception
What is wrong with the following code? String s; System.out.println(s);
You cannot print a String without setting its value; the string, s, needs to be initialized with a value before it can be printed.
Which of the following are examples of primitive data types? a) boolean b) char c) Scanner d) int e) Arrays
a, b, d
What will happen if the computer fails to compile your code?
an error message will be displayed
What color text signifies that the code is a comment and won't be run?
green
Why does strings take up so much space in memory?
have to store each character separately
If we input a string when doing calculations, we'll get an
input mismatch exception, or runtime error (it will compile, but crash)
List primitive data types
int, double, boolean
What does the import statement do? importJava.util.Scanner;
introduces the class before the rest of the code
What is concatenation>
joining literal strings with variables (use +)
In the following code, what do the periods act as? System.out.println(" ");
member selectors (separate)
What are the rules for naming variables?
no spaces, must start with a letter, no reserve words
To output: Hello There You would need to use: System.out.______("Hello") System.out.print("There");
println
What is wrong with the following code? String s = nextLine( );
should be: String s = scan.nextLine( );
String variables can be concetenated with
string literals
What does the Scanner class allow us to do?
take user input and incorporate it into programs
What are you doing when you declare a variable?
telling computer to allocate space in memory for something (tell the name so it knows how to find it and the type so it knows how much space to set aside)
In the following code, what does System represent? System.out.println(" ");
the class name
What will be printed to the screen when the following code runs? String s1 = "three"; String s2 = "five"; System.out.print(s1 + s2);
threefive
why are classes capitalized?
to separate them from variables
True or False. All statements in Java end with a semicolon
true
True or False. Reference data types can store several different types of info and thus don't get stored directly in the variable like primitive data types are
true
True or False. Strings are used for anything we don't do calculations with
true
True or False. Variables are like containers in memory
true
When variables are assigned a type, what is the computer told about?
what possible values can be stored (how big a box of memory should be allocated); what operations can be performed on the data
Different data types can store different amounts of data. List the data types in order from largest to smallest
String, double, int, boolean
Strings are specified by
" "
What is used to mark a single line comment?
/ /
What is used to mark a block comment?
/* */
What is the code used to print something on the screen?
System.out.print(" ");
What is the code you should print when you want to scan input?
Scanner scan = new Scanner(System.in);
In the following code, what is the equal sign? Scanner scan = new Scanner(System.in);
assignment operator (initializes or changes the value of the variable)
What is the largest value you can store in a long variable? a) 1,024 b) 9,223,372,036,854,775,807 c) 2,147,483,647 d) 256
b
What is wrong with the code below? int guess = 42.07; a) Nothing the code is fine as-is. b) You cannot store a double in an int variable. c) The type should be "Double" not "int". d) You cannot store an int in a double variable. e) The variable should be a String. f) The type of the variable should be a float or a double.
b and f
Suppose we had declared a String variable called myString in a program. Which of the following lines would assign this variable to contain the String "Hi there"? a) set myString to "Hi there" b) myString = "Hi there"; c) "Hi there" = myString; d) String myString "Hi there" e) myString: "Hi there";
b) myString = "Hi there";
What data type should you use to hold whether a person passed or failed a class?
boolean
Which of the following would Java interpret as being a String? a) <p>Hello World</p> b) 'Hello World' c) "Hello World" d) ```Hello World```
c) "Hello World"
the directions we give in programming are called
commands
what is instantiating?
creating objects (objects created from the Scanner class)
Which of the following correctly describes the difference between reference data types and primitive data types? a) Unlike primitive data types, reference data types cannot be stored by a variable. b) Primitive data types contain multiple pieces of data, while reference types only store one value at a time. c) Primitive data types cannot be read by a Scanner object and stored using a variable. d) Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored.
d) Primitive data types are stored directly in a variable, while reference data type variables store a memory reference to where the data is stored.
Which line of code correctly declares and initializes the variable x to a value of 3? a) int x : 3; b) Integer x = new Integer(x); c) x = 3 d) int x = 3;
d) int x = 3;
Which of the following is a legal variable name in Java? a) a name b) n.1 c) 1test d) value e) String
d) value
________ is the process of finding and eliminating errors in code.
debugging
Which of the following is NOT a valid string in Java? a) "hello" b) "@#$" c) "a" d) "123" e) In Java, these are all valid strings
e) In Java, these are all valid strings