CS 1331 Exam 1 Study Guide PDF

Title CS 1331 Exam 1 Study Guide
Course Intro-Object Orient Prog
Institution Georgia Institute of Technology
Pages 12
File Size 546.7 KB
File Type PDF
Total Downloads 52
Total Views 128

Summary

History of Java, java math, data types, for and while loops, arrays, scanners, switch statements, continue and break statements, conditionals, nested loops, methods...


Description

Exam 1 Study Guide 1.

Expression Table

If output is is a String, surround it with “”, if output is char surround it with single quotes ‘’, floats should have f or F at the end, longs should have L or l (preferably upper case since l looks like a one) doubles and ints do not require letter since default of floating point values are doubles and default type of integer based is int

Expression

Output

Datatype

7/2 + 9.5/2

7.75

double

1.2 + 5.3f

6.5

double

“pizza”.substring(1,5)

“izza”

string

(float)4.0 + (int)1.3

5.0F

float

1L + 2.0

3.0

double

3 + 6L

9L

long

(2 + 3 > 4) || 3 < 1 && false

true

boolean

3.0 + 1 + “Hello” + 2 + 5

“4.0Hello25”

string

String.format(“%3.3f”, 1.23456)

“1.235”

string

String.format(%s”, “1331”)

“1331”

string

String.format(“%d + %.0f”, 1, 3.0)

“1 + 3”

string

3.4 == 3.4f? “true” : “false”

“false”

string

2.

3.

(int)5.5/3 - 2

-1

int

8 == 9 && 3 < 2 || 8 < 9

true

boolean

“burger”.equals(“BuRgEr”)

false

boolean

true || 5 != 5

true

boolean

false && 5 == 5

false

boolean

True/False 2.1. true (only false when you do include the brackets) 2.2. false 2.3. false, String is an object type, null default value 2.4. true, “7” is a string 2.5. false, java is statically typed 2.6. false 2.7. false, compiles but does not run 2.8. false, it is arr.length without parenthesis 2.9. false, you cannot go from double to int 2.10. false, “array dimension missing” 2.11. false, “incompatible types: possible lossy conversion from double to int” 2.12. false, “index -1 out of bounds” 2.13. true 2.14. false, boolean default value is false Short Answer 3.1. public static void main(String[] args) byte, short, int, float, long, double, char, boolean 3.2. 3.3. javac Avocado.java 3.4. java Avocado 3.5. Salt Salt Salt 3.6. int i = 5; while (i > 0) { System.out.print(“Salt “); i -= 2; } 3.7. String str = “working hard”; switch (str) { case “WORKING HARD”: System.out.println(“YAY”); break; case “working”: System.out.println(“hard”); break; default:

System.out.println(“error”); break;

3.8.

3.9.

3.10.

} (technically does not need break; for final case) for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.println(“Hello”); }} for (int num : nums) { System.out.println(num); } String [][] i = {{“Sally”, “John”}, {“Danny”, “Sarah”}}; for (String[] nameList : i) { for (String name : nameList) { System.out.println(name); } } // output is Sally John Danny Sarah each on their own line // example from recitation for (String[] row : i) { for (String word : row) { System.out.println(word); } }

3.11.

// if question asked about int array for (int [] num: arr) { for(int i: num) { System.out.println(i); } } public class JaggedArray { public static void main(String[] args) { int arr[][] = new int[2][]; // first row 3 columns arr[0] = new int[3]; // second row 4 columns arr[1] = new int[4]; // initializing array int count = 1; for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < arr[i].length; j++) { arr[i][j] = count++; } } // printing out final array for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (j == 0) { System.out.print(“|”); } System.out.print(arr[i][j] + “|”); } System.out.println(); } } } final output

3.12. 3.13.

4.

5.

out of bound error, 0.0 for (int col = 0; col < arr[0].length; col ++) { for (int row = 0; row < arr.length; row ++) { System.out.println(arr[row][col]); } } 3.14. for (int row = 0; row < arr.length; row ++) { for (int col = 0; col < arr[row].length; col ++) { System.out.println(arr[row][col]); } } Scanner 4.1. error : “cannot find symbol”, we need to add keyword “new” before Scanner 4.2. error : “incompatible types”, name is a string, should use next() or nextLine() 4.3. error : “user input for fact is skipped”, should use next() instead because the newline character from nextInt() is carried over, therefore if you use nextLine() execution stops right when it starts since nextLine() stops at a newline character Switch Statements 5.1. The banana flavor is the best 5.2. I love chocolate The banana flavor is the best 5.3. I don’t like candy

5.4. 5.5.

Licorice is amazing I prefer skittles Other Monday Other Wednesday Monday Other Monday Tuesday Wednesday Monday Other Tuesday Wednesday Monday Other

L1 - L9 Practice Problems L1. What benefits does Java’s hybrid approach to compilation and interpretation offer? the compiler generates bytecode and bytecode comes close to the low-level of machine code, when the bytecode is generated, an interpreter can translate the bytecode without a lot of translation costs since bytecode is already at low level by incorporating both compilers and interpreters, Java can provide the platform independence of interpreted languages while minimizing their inefficiencies. On top of this, it incorporates certain creative optimizations that also improve the speed of interpreting bytecode L2. Give an example of what variables and methods a class called CS1331 would have and list what their purpose is (minimum 2 for each). method 1 : ClassRoster variable : int sum method 1 could print out the sum or total of how many students there are in the class method 2 : MadeAnA variable : int gradeA method 2 would be given an array of grades and for each A grade in the array the int variable would keep count of how many students made an A L3. Give an example of a compile time error and a run time error. For the runtime error, give a specific name of an exception. What is the difference between these two? compile time error : char letter = “A”; (char uses single quotes not double) runtime error : assuming array is of length 4, if we tried to call array[6], this would be a “an out of bounds error” compiler errors are due to syntax, while runtime errors are due to semantics L3. List two primitive types

any of the following would work : byte, short, int, long, float, double, char, boolean L3: What happens here? Why? Hint: Remember that there’s more to life than just inside this method.

error: variable sadge might not have been initialized variables within methods are not initialized with a default value unless they are instance or static variables or if within an array vv so it is assigned the default value L3: Can you explain the output of these lines?

there is an error caused by int y = x because you cannot go from a double to an int this would cause the error “incompatible types: possible lossy conversion from double to int"

L4: Strings What does this print out? Note: Syntax matters in this course; try to write down all the code first then see if it compiles and runs as expected after you have your final answer.

?Hello World! Hello World! L4: What gets printed out from these lines?

40howdy L5: Scanner

TES will win worlds 2020 DWG JDG are the losers // note there is a space before DWG are the losers (next() does not take away \n when enter is pressed, so nextLine() takes that as input) L5: Explain how next() and nextLine() work. next() can read the input only till the space. It can't read two words separated by space. Also, next() places the cursor in the same line after reading the input. nextLine() reads input including space between the words (that is, it reads till the end of line) Once the input is read, nextLine() positions the cursor in the next line

L5:How would you use printf to round 3.14159265453 to 4 decimal places? System.out.printf(“%.4f”, 3.14159265453); L6: Conditionals

prints out Java I guess... L6: What gets printed out here? Why?

3 2 because it increments after, if the ++ was on the left then println would be executed L6: Convert this into a switch statement.

switch (s) { case “Georgia”: System.out.println(“Hawks”); break; case “New York”: System.out.println(“Knicks”); break; case “California”: System.out.println(“Lakers”); break:

default: System.out.println(“My state doesn’t” + “have a basketball team”); break; the } System.out.println(“The if has been converted to a switch”); } (also final case does not need the break; and will still print the if has been converted line) L7: Nested Loops What is printed out with this chunk of code? Can you convert this into a while loop?

output * ** *** **** ***** ****** // while loop int i = 1; int j = 1; while (i...


Similar Free PDFs