1To declare a 1-D double array called a without any values:
double[] a;
double a[] ;
2To declare a 1-D double array called a with a length of 10:
double[] a = new double[10];
3To declare a 1-D int array called a without any values:
int[] a;
4To declare a 2-D int array with 10 rows and 11 columns called b:
int[][] b = new int[10][11];
5To declare a 1-D String array called SUITS:
String[] SUITS;
6To declare an initialize a 1-D String array called SUITS:
String[] SUITS = {
"Clubs", "Diamonds, "Hearts", "Spades"
};
7To get the length of an array called a:
a.length;
8What does the first number in the bracket of an array represent? Ex. int[] arr = new int[n]
n always = row number
9What does the second number in the bracket of an array represent? Ex. int[] arr = new int[n][k]
k always = column number
10Questions. Is this code ok?
int n = 1000;
yes,
11Questions. Is this code ok?
int[] a = new int[n*n*n*n];
no – There is not enough RAM to assign tera byte (10^12) bytes of memory
12How much memory is allocated to one position of an array?
4 bytes for each position + 16 bytes for overhead
13How many bytes are added for overhead?
16 bytes
14If an array has 10 rows, how much memory is it using?
4 bytes * 10 = 40 bytes + 16 bytes for overhead
15Question. Is this ok?
int[] a = new int[10];
for (int i = 0; i < 10; i++)
a[i] = i * i;
The code would work fine.
16Question. What does this code print?
int[] a = { 1, 2, 3 };
int[] b = { 1, 2, 3 };
System.out.println(a == b);
It prints false b/c a refers to the address of an array called a while b refers to the address of an array called b.
They are allocated to different places, meaning that they're never the same.
17What is this code doing?
READ num1
READ num2
READ num3
IF (num1 >= 0) OR (num2 >= 0) OR (num3 >= 0) THEN
DISPLAY ERROR
ELSE
SET largest TO num1
IF (num2 > largest) THEN
SET largest TO num2
ENDIF
IF (num3 > largest) THEN
SET largest TO num3
ENDIF
DISPLAY largest
ENDIF
If num1, num2, or num3 is greater than or equal 0, it displays an ERROR. However, if any of them are less than 0, it finds the largest value from num1, num2, and num3, and sets the variable called largest = to that variable.
18What is the min number of operations?
READ num1
READ num2
READ num3
IF (num1 >= 0) OR (num2 >= 0) OR (num3 >= 0) THEN
DISPLAY ERROR
ELSE
SET largest TO num1
IF (num2 > largest) THEN
SET largest TO num2
ENDIF
IF (num3 > largest) THEN
SET largest TO num3
ENDIF
DISPLAY largest
ENDIF
7 operations
19What is the max number of operations?
READ num1
READ num2
READ num3
IF (num1 >= 0) OR (num2 >= 0) OR (num3 >= 0) THEN
DISPLAY ERROR
ELSE
SET largest TO num1
IF (num2 > largest) THEN
SET largest TO num2
ENDIF
IF (num3 > largest) THEN
SET largest TO num3
ENDIF
DISPLAY largest
ENDIF
10 operations
20What data type is the result of this expression?
(1 + 2 + 3 + 4) / 4.0
double
21What's the result of this expression?
(1 + 2 + 3 + 4) / 4.0
2.5
22What data type is the result of this expression?
Math.sqrt(4)
double
23What's the result of this expression?
Math.sqrt(4)
2.0
24What data type is the result of this expression?
"1234" + 99
String
25What's the result of this expression?
"1234" + 99
"123499"
26What data type is the result of this expression?`
11 * 0.25
double
27What's the result of this expression?
11 * 0.25
2.75
28What data type is the result of this expression?`
(int) 11 * 0.25
double
29What's the result of this expression?
(int) 11 * 0.25
2.75
30What data type is the result of this expression?`
11 * (int) 0.25
int
31What's the result of this expression?
11 * (int) 0.25
0
32What data type is the result of this expression?`
(int) (11 * 0.25)
int
33What's the result of this expression?
(int) (11 * 0.25)
2
34What data type is the result of this expression?`
(int) 2.71828
int
35What's the result of this expression?
(int) 2.71828
2
36What data type is the result of this expression?`
Math.round(2.71828)
long
37What's the result of this expression?
Math.round(2.71828)
3
38What data type is the result of this expression?`
(int) Math.round(2.71828)
int
39What's the result of this expression?
(int) Math.round(2.71828)
3
40What data type is the result of this expression?`
Integer.parseInt("1234")
int
41What's the result of this expression?
Integer.parseInt("1234")
1234
42What does the operation == do?
checks whether or not 2 values are equal to each other
43True or false: 2 ==2
true
44True or false: 2 == 3
false
45What does the operation != do?
checks if 2 values are not equal to each other
46What does < mean?
less than
47What does <= mean?
less than or equal to
48What does > mean?
greater than
49What does >= mean?
greater than or equal to
50What does this code do?
int n = 123456789;
int m = 0;
while (n != 0) {
m = (10 * m) + (n % 10);
n = n / 10;
}
It's taking the number in the variable called n, creating the same number in reverse order, and setting it as the value for variable m
51What does a keyboard take in?
text
52What does a scanner take in?
physical object
53What does a webcam take in?
physical object
54What does a mouse take in?
click
55What does a touch monitor take in?
touch input
56What does a microphone take in?
sound
57What does a stylus take in?
handwriting
58What are some possible input sources?
keyboard, camera, text or binary file, mouse, audio device, touch, DVD/CD/Disk, Remote, Game controller
59What are some possible output devices?
monitor/TV, speaker/headphones/headset, printer, etc.
60When a key on the keyboard is pressed, it does what?
it sends a signal to a program through a buffer
61When a program is processing the output, it does what?
it buffers and sends the output to a device
62How do you handle input in a program?
By using StdIn or file redirection
63Stdln stands for?
Standard In
64What does StdIn do?
it interactively enters input as requested by the program
65What does file redirection do?
It uses cmd to run a program using a given input
66cmd/c 'java PlayThatTube < data/tomsdiner.txt'
Ex. of file redirection
Takes in the file called tomsdiner.txt and uses it as an input in the java file called PlayThatTube
67What's the format of a StdOut.printf command?
StdOut.printf("%field_width_#.precision_type conversion_specification", #_to_print)
68What does StdOut.printf("%7.5f", Math.PI) do?
It prints out the number pi in the span of 7 spaces, with 5 decimals after the dot, and as a double value
69How to print out a int value in StdOut.print?
use d
70How to print out a double value in StdOut.print?
use f
71How to print out a doubke value in StdOut.print through exponential notation?
use e
72How to print out a String value in StdOut.print?
use s
73How to print out a Boolean value in StdOut.print?
b
74What does StdOut.printf("%14d", 512) print when put in quotes?
" 512"
75What does StdOut.printf("%-14d", 512) print when put in quotes?
"512 "
76What does StdOut.printf("%14.2f", 1595.1680010754388) print when put in quotes?
" 1595.17"
77What does StdOut.printf("%.7f", 1595.1680010754388) print when put in quotes?
"1595.1680011"
78What does StdOut.printf("%14.4e", 1595.1680010754388) print when put in quotes?
" 1.5952e+03"
79The value from StdOut.printf is always printed from _____ to _____ unless there's a ____
right to left, unless there'a -
80What does StdOut.printf("%14s", "Hello, World") print when put in quotes?
" Hello, World"
81What does StdOut.printf("%-14s", "Hello, World") print when put in quotes?
"Hello, World "
82What does StdOut.printf("%-14.5s", "Hello, World") print when put in quotes?
"Hello "
83What does StdOut.printf("%b", true) print when put in quotes?
"true"
84What does isEmpty() do?
checks if the value in () is empty space or not
85What's the return type of isEmpty()?
boolean
86What does readInt() do?
reads a value, convert it to int, & return it
87What's the return type of readInt() ?
int
88What does readDouble do?
reads a value, converts it to a double, & returns it
89What's the return type of readDouble() ?
double
90What does readBoolean() do?
reads a value, converts it to a boolean, and returns it
91What's the return type of readBoolean() ?
boolean
92What does readString() do?
reads a value & returns it as a String
93What's the return type of readString() ?
String
94What does hasNextChar() do?
it checks if the value has any remaining characters
95What's the return type of hasNextChar() ?
boolean
96What does readChar() do?
reads a character from standard input & returns it
97What 's the return type of readChar() ?
char
98What does hasNextLine() do?
checks if the standard input has another line
99What's the return type of hasNextLine()?
boolean
100What does readLine() do?
reads the rest of the line & returns it as a String
101What's the return type of readLine() ?
String
102What does int[] readAllInts() do?
reads all remaining values & returns them as an int array
103What does double[] readAllDoubles() do?
reads all remaining values & returns them as a double array
104What does String[] readAllStrings() do?
reads all remaining values & returns them as a String array
105What does String readAll() do?
reads the rest of the input & returns it as a String
106To redirect the output of a java file:
java fileName.java input_value > outputFileName
107To input the values in a file into a java file:
java fileName < inputFilName
108The arrow (< or >) signifies what in the java filname > fileName format?
The < or > points towards where the file is being inputted
109Does it take longer to write in a terminal or to print to a file?
It takes longer to write in a terminal
110Why does it take longer to write in a terminal?
It needs to bufer the output from memory/program to terminal
111What does piping do?
it sends output from 1 program as input to another program; used in Linux systems
112What's the format of piping w/ java programs?
java fileName | sort
113To use file redirection in VSCode:
cmd /c 'java fileName.java < inputFileName'
114What do byte streams do?
they perform input & output of 8-bit bytes
115What are the most frequently used classes?
FileInputStream & FileOutputStream
116A pixel has how many bytes?
3
117In what order are colors designated in a pixel?
RGB
118How big or small can #s be for each color in a pixel?
0-255
119What does StdOut.print(String S) do?
puts S on the output stream
120What does StdOut.println() do?
puts a newline on the output stream
121What does StdOut.println(String S) do?
puts S on the output stream, and then a newline on the output stream
122What does StdOut.printf(String f...) do?
puts a formatted version of the value in F
123What does StdDraw.line(double x0, double y0, double x1, double y1) do?
draw a line from x0, y0 to x1, y1
124What does StdDraw.point(double x, double y) do?
draw a point at x, y
125What does StdDraw.setCanvasSize(int w, int h) do?
create a canvas in a screen window of width w & height h in pixels
126What does StdDraw.setXScale(double x0, double x1) do?
reset x-scale to (x0, y0)
127What does StdDraw.setYScale(double y0, double y1) do?
reset y-scale to (y0, y1)
128What does StdDraw.setPenRadius(double radius) do?
set pen radius to the variable called radius
129Static methods can have:
multiple arguments
130How many static methods can you have?
As many as you want in a .java file. They're independent & can appear in any order in the file. They can also call other methods in the same file or methods in the Java library.
131using the same name for 2 static methods whose signatures differ
Overload
132Whenever the first return statement is run in a method:
that value is returned to the main method where it was called
133Static methods allow
methods to be called w/o an object from the class Ex. Math.abs(12)
134What does a call stack do?
stores that state of the program as the program makes calls to other functions.
135In what order are the statements in the call stack called?
foo1() {foo2(n); /"next statement S1 */}
foo2(int n) {foo3(n); next statement S3"/}
foo3(int n) {...}
main() {foo1(); next statement S2 */
Call stack: main() foo1() foo2() foo3() top of stack*/
S2 S1 S3 no next
136What is a stack?
location in the main memory that's specifically allocated for function execution by the compiler
137Where do hacker attacks often originate?
from attacking the stack
138public static int foo(int x, double y);
private int foohelper(int x, double y);
What does public mean in this method signature?
the method can be called by any program
139public static int foo(int x, double y);
private int foohelper(int x, double y);
What does static mean in this method signature?
the method can be called w/o an object by just using class name
140public static int foo(int x, double y);
private int foohelper(int x, double y);
What does int mean in this method signature?
int is the return type
141public static int foo(int x, double y);
private int foohelper(int x, double y);
What does foo mean in this method signature?
foo is the method name
142public static int foo(int x, double y);
private int foohelper(int x, double y);
What does int x & double y mean in this method signature?
Those are the parameters or arguments taken in by the method signature
143public static int foo(int x, double y);
private int foohelper(int x, double y);
What does it mean when static isn't used in the method signature?
it means that the class can't be called outside of the class
144take input from outside but behave like a local variable
Parameter variables
145defined only for tasks inside the function
local variable
146program that calls a given library method
client
147contract b/w the client & the implementation on what the method was intended to do
API
148describes the java code that implements the methods in an API
implementation
149recusion
function that calls itself
150a recurse method has the following:
base case
induction step
151base case
proves that statement true for some specific value or values (usually 0 or 1)
152induction step
assumes that the statement is true for all positive integers less than n, use the fact to prove it true for n