DSA QUIZ 1 WITH COMPLETE
SOLUTIONS
main method - Correct Answers -public static void main(String[] args)
How Java works - Correct Answers --You write Xyz.java files
-Your file, Xyz.java, must define the class Xyz
-The compiler writes Xyz.class files at compile time
-Java (JVM) reads Xyz.class
random - Correct Answers -*Random( ) Creates a random number generator.
Random random = new Random()
*int die = random.nextInt( int number ); 0 to number is max but exclusive
Scanner - Correct Answers -Scanner scan = new Scanner( System.in );
scanner methods - Correct Answers -nextDataType( ) returns the next token in the input
stream as a dataType. dataType can be byte, int, short, long, float, double, or boolean
next( ) returns the next token(word) in the input stream as a String nextLine( ) returns
the remainder of the line as a String
*come after new Scanner() and prompt as scannername.next...
** can't do nextChar so read as String and use atChar(0)
Commenting - Correct Answers -// beginning of one line
/* beginning of multiple */ end of multiple
/** special comment that can be turned into documentation */
DataType parts - Correct Answers -Domain
-what values can be stored?
-what is the range of values?
-what is a legal value? (bounds)
-determines how much memory to reserve (size)
Operations
-what can you do?
-what can't you do?
-determines operations
Static Typing - Correct Answers --All variables must be declared with a type before they
are used
, -A variable cannot change types! (static = does not change)
-Data type determines allowed operations
Primitive Data Types - Correct Answers -Whole number integers Differ in size (number
of digits):
byte
short
int
long
Decimal numbers Differ in precision (number of digits) :
float
double
boolean: True / False
char only: A single character
Names - Correct Answers -Variable names are case-sensitive:
Donkey != donkey
Classes are TitleCase
Variables/methods are camelCase
identifiers/Keywords - Correct Answers --Made up by Java
-Must start with a letter and contain only letters, digits, and underscores
-Case sensitive
final keyword - Correct Answers -variable's literal cannot be changed
Every expression has - Correct Answers -data type
value
right (value) must fit in left (data type)
underscores - Correct Answers -use as commas in numbers
When numbers do not fit allocated memory space - Correct Answers -data can be lost
casting makes sure data doesn't get lost
rounding when double to int - Correct Answers -down
char - Correct Answers -single letter
treated as int and can do operation with number equivalent
Control Structures - Correct Answers -Sequential
Branching: break return
SOLUTIONS
main method - Correct Answers -public static void main(String[] args)
How Java works - Correct Answers --You write Xyz.java files
-Your file, Xyz.java, must define the class Xyz
-The compiler writes Xyz.class files at compile time
-Java (JVM) reads Xyz.class
random - Correct Answers -*Random( ) Creates a random number generator.
Random random = new Random()
*int die = random.nextInt( int number ); 0 to number is max but exclusive
Scanner - Correct Answers -Scanner scan = new Scanner( System.in );
scanner methods - Correct Answers -nextDataType( ) returns the next token in the input
stream as a dataType. dataType can be byte, int, short, long, float, double, or boolean
next( ) returns the next token(word) in the input stream as a String nextLine( ) returns
the remainder of the line as a String
*come after new Scanner() and prompt as scannername.next...
** can't do nextChar so read as String and use atChar(0)
Commenting - Correct Answers -// beginning of one line
/* beginning of multiple */ end of multiple
/** special comment that can be turned into documentation */
DataType parts - Correct Answers -Domain
-what values can be stored?
-what is the range of values?
-what is a legal value? (bounds)
-determines how much memory to reserve (size)
Operations
-what can you do?
-what can't you do?
-determines operations
Static Typing - Correct Answers --All variables must be declared with a type before they
are used
, -A variable cannot change types! (static = does not change)
-Data type determines allowed operations
Primitive Data Types - Correct Answers -Whole number integers Differ in size (number
of digits):
byte
short
int
long
Decimal numbers Differ in precision (number of digits) :
float
double
boolean: True / False
char only: A single character
Names - Correct Answers -Variable names are case-sensitive:
Donkey != donkey
Classes are TitleCase
Variables/methods are camelCase
identifiers/Keywords - Correct Answers --Made up by Java
-Must start with a letter and contain only letters, digits, and underscores
-Case sensitive
final keyword - Correct Answers -variable's literal cannot be changed
Every expression has - Correct Answers -data type
value
right (value) must fit in left (data type)
underscores - Correct Answers -use as commas in numbers
When numbers do not fit allocated memory space - Correct Answers -data can be lost
casting makes sure data doesn't get lost
rounding when double to int - Correct Answers -down
char - Correct Answers -single letter
treated as int and can do operation with number equivalent
Control Structures - Correct Answers -Sequential
Branching: break return