CS302 EXAM 1 WITH QUESTIONS
AND CORRECT ANSWERS
given: String[] list = {"a", "b", "c"} ; i = 0;
what does list.length vs. list[i].length() return? - ANSWER Array.length is a
property of arrays that returns the number of elements in an array so list.length
returns 3
String.length() is a method of strings that returns the number of characters in a
string, and because 'list' is a string type array, list[i].length() returns the number
of characters at index i in array list so returns 1 ("a" = 1 char)
return type of Math.pow(x, y) - ANSWER double
algorithms - ANSWER a step-by-step set of operations to be performed
- procedure for solving a problem in terms of the actions to be executed and the
order in which those actions are to be executed.
- sequence of steps taken to solve a problem
abstraction - ANSWER a technique for managing complexity
escape sequences - ANSWER special character sequences used within a string
- starts with \
- within " "
\n = newline
\t = tab
\' = '
\" = "
\\ = \
variables - ANSWER name areas of computer memory, declare before use,
declare type of data, initialize
= vs. == - ANSWER = : assignment, assigns values on right to variable on left
, == : conventional definition of equals, or "is"
read in values - ANSWER 1. import java.util.Scanner; // imports scanner
2. Scanner input = new Scanner(System.in); // enables scanner to read input
from System.in
3. int variable = input.nextInt(); // scanner reads next int from System.in
String name = input.nextLine(); // scanner reads next line of input, next string
~same as~
Scanner scnr = new Scanner(System.in);
int variable = scnr.nextInt();
String name = scnr.nextLine();
.nextLine() vs .next() - ANSWER both used to get next string from input,
however -
.next() reads up to the first whitespace(space, newline, tab, or user press
ENTER), reads first non-whitespace thing
-- works when input is one word or when only want first word of input
-- places cursor(scanner) on same line where left off --> if used again, tosses the
whitespace it stopped at and returns next non-whitespace thing
- x = scnr.next()
x.equals("\n") WILL NEVER HAPPEN, scnr.next CANNOT be "\n"
if user input looks like:
1
G
then .next() first returns 1, another .next() starts on the same line and skips any
whitespace(including newline) and returns next non-whitespace thing
.nextLine() reads entire user input on a line up to newline made when user
presses enter or \n when reading from non user input (if user input contains \n
for some reason nextLine() will read \n whereas it would otherwise stop there)
-- moves cursor(scanner) to next line
-- returns the rest of the line, even if its nothing ("")
---BOTH TOSS OUT NEWLINE CHARACTERS
.equals vs. == - ANSWER both return true if the two things being compared
are the same however
AND CORRECT ANSWERS
given: String[] list = {"a", "b", "c"} ; i = 0;
what does list.length vs. list[i].length() return? - ANSWER Array.length is a
property of arrays that returns the number of elements in an array so list.length
returns 3
String.length() is a method of strings that returns the number of characters in a
string, and because 'list' is a string type array, list[i].length() returns the number
of characters at index i in array list so returns 1 ("a" = 1 char)
return type of Math.pow(x, y) - ANSWER double
algorithms - ANSWER a step-by-step set of operations to be performed
- procedure for solving a problem in terms of the actions to be executed and the
order in which those actions are to be executed.
- sequence of steps taken to solve a problem
abstraction - ANSWER a technique for managing complexity
escape sequences - ANSWER special character sequences used within a string
- starts with \
- within " "
\n = newline
\t = tab
\' = '
\" = "
\\ = \
variables - ANSWER name areas of computer memory, declare before use,
declare type of data, initialize
= vs. == - ANSWER = : assignment, assigns values on right to variable on left
, == : conventional definition of equals, or "is"
read in values - ANSWER 1. import java.util.Scanner; // imports scanner
2. Scanner input = new Scanner(System.in); // enables scanner to read input
from System.in
3. int variable = input.nextInt(); // scanner reads next int from System.in
String name = input.nextLine(); // scanner reads next line of input, next string
~same as~
Scanner scnr = new Scanner(System.in);
int variable = scnr.nextInt();
String name = scnr.nextLine();
.nextLine() vs .next() - ANSWER both used to get next string from input,
however -
.next() reads up to the first whitespace(space, newline, tab, or user press
ENTER), reads first non-whitespace thing
-- works when input is one word or when only want first word of input
-- places cursor(scanner) on same line where left off --> if used again, tosses the
whitespace it stopped at and returns next non-whitespace thing
- x = scnr.next()
x.equals("\n") WILL NEVER HAPPEN, scnr.next CANNOT be "\n"
if user input looks like:
1
G
then .next() first returns 1, another .next() starts on the same line and skips any
whitespace(including newline) and returns next non-whitespace thing
.nextLine() reads entire user input on a line up to newline made when user
presses enter or \n when reading from non user input (if user input contains \n
for some reason nextLine() will read \n whereas it would otherwise stop there)
-- moves cursor(scanner) to next line
-- returns the rest of the line, even if its nothing ("")
---BOTH TOSS OUT NEWLINE CHARACTERS
.equals vs. == - ANSWER both return true if the two things being compared
are the same however