Exam 2 (Weeks 5-8)

152 flashcardsby Sg
Study these flashcardsFree · spaced repetition · iPhone, Android, Mac & web

All cards

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