102
Exam 1 Review

Exam 1 Review

Embed Size (px)

DESCRIPTION

Exam 1 Review. Exam 1. A lot of you found the exam to be very difficult as evidenced by some low scores. If you did not do well on the exam, please don’t panic. You will have the chance to make it up. How? We’ll get back to you on that. We are figuring out the details. - PowerPoint PPT Presentation

Citation preview

Exam 1 Review

Exam 1• A lot of you found the exam to be very difficult as evidenced by some low scores.

• If you did not do well on the exam, please don’t panic. You will have the chance to make it up.• How? We’ll get back to you on that. We are figuring out the details.• In the meantime, learn from your mistakes! Take notes and ask questions as we review the exam.

• Writing code outside of Eclipse is hard! We will work to ensure you get more practice.

• Why is it important to be able to code without an IDE?• Demonstrates that you’ve grasped the concepts and don’t need a crutch to produce good code.• Coding interviews often take place on whiteboards. This is a good skill to develop early on.• I code on paper and whiteboards constantly. It’s often easier when sharing ideas with other developers.• AP exam• In-class exams

Section A: Question 1All the following are primitive data types: int, char, double, boolean, String

a) TRUEb) FALSE

Section A: Question 1All the following are primitive data types: int, char, double, boolean, String

a) TRUEb) FALSE

Section A: Question 2The following is an example of an implicit cast:int a = (int) (5.0 / 9.0);

a) TRUEb) FALSE

Section A: Question 2The following is an example of an implicit cast:int a = (int) (5.0 / 9.0);

a) TRUEb) FALSE

Explicit cast vs. implicit castAn implicit cast occurs when the cast is automatically done by the compiler and no code is required.

int i = 3;

double d = 3;

public static compute(double d) {…}

compute(42);

An explicit cast requires code from the programmer to inform the compiler of the cast.

int i = (int)(Math.sqrt(2));

Section A: Question 3The concatenation operator is the plus sign.

a) TRUEb) FALSE

Section A: Question 3The concatenation operator is the plus sign.

a) TRUEb) FALSE

Section A: Question 4Adding a double and an int implicitly converts the int variable to a double and the sum is a double.

a) TRUEb) FALSE

Section A: Question 4Adding a double and an int implicitly converts the int variable to a double and the sum is a double.

a) TRUEb) FALSE

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a) a = -36, b = 9b) a = -36, b = 50c) a = 3, b = 11d) a = 9, b = 50e) a = 9, b = 11

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 0

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 0

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 0

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 0

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 1

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 5i = 1

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 9 b = 6i = 1

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 8 b = 6i = 1

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 8 b = 6i = 2

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 8 b = 6i = 2

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 8 b = 8i = 2

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 6 b = 8i = 2

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 6 b = 8i = 3

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 6 b = 8i = 3

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 6 b = 11i = 3

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 3 b = 11i = 3

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 3 b = 11i = 4

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a = 3 b = 11i = 4

Section A: Question 5int a = 9;

int b = 5;

 

for (int i = 0; i <= a; i++) {

b = b + i;

a = a - i;

}

 

a) a = -36, b = 9b) a = -36, b = 50c) a = 3, b = 11d) a = 9, b = 50e) a = 9, b = 11

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

 

a) 4 3 5b) 4 3 5 4c) 4 3 2 5 4 3d) 1 3 1 2 2 3e) 1 3 1 2 2 3 2 2

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = output:

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = output:

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 3output:

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 3output:

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 3output: 4

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 2output: 4

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 2output: 4

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 2output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 1output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 1 j = 1output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2j = output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = 3output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = 3output: 4 3

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = 3output: 4 3 5

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = 2output: 4 3 5

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 2 j = 2output: 4 3 5

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 3 j = output: 4 3 5

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

i = 3 j = output: 4 3 5

Section A: Question 6for (int i = 1; i < 3; i++) {

for (int j = 3; j > i; j--) {

System.out.print(i+j + " ");

}

}

 

a) 4 3 5b) 4 3 5 4c) 4 3 2 5 4 3d) 1 3 1 2 2 3e) 1 3 1 2 2 3 2 2

Section A: Question 7Which code will produce the following output?

The quick brown fox

jumped over the lazy dog.

I. System.out.print(“The quick brown fox\njumped over the lazy dog.”);

II. System.out.print(“The quick brown fox”);

System.out.println(“jumped over the lazy dog.”);

III. System.out.print(“The quick brown fox”);

System.out.print(“jumped over the lazy dog.”);

 

Section A: Question 7Which code will produce the following output?

The quick brown fox

jumped over the lazy dog.

I. System.out.print(“The quick brown fox\njumped over the lazy dog.”);

II. System.out.print(“The quick brown fox”);

System.out.println(“jumped over the lazy dog.”);

III. System.out.print(“The quick brown fox”);

System.out.print(“jumped over the lazy dog.”);

 

Section A: Question 7Which code will produce the following output?

The quick brown fox

jumped over the lazy dog.

a) None

b) I only

c) II only

d) III only

e) I and II only

f) I, II, and III

 

Section A: Question 8What does the following method return?

public static int method() {

int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a;

a) 1b) 4c) 8d) 9

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a =

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 1

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 1

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 2

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 3

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 3

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 8

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 9

Section A: Question 8int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a; 

a = 9

Section A: Question 8What does the following method return?

public static int method() {

int a;

for (a = 1; a <= 3; a++) {

a = (int)Math.pow(2, a);

}

return a;

a) 1b) 4c) 8d) 9

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a) 1 2 2 1b) 2 2 1 2c) 2 1 2 1d) 2 2 2 2e) 1 2 2 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output:

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output:

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output:

a = 1b = 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output:

a = 2b = 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output:

a = 2b = 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output: 2 2

a = 2b = 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a = 1b = 2output: 2 2 1 2

Section A: Question 9public class Program { public static void main(String[] args) { int a = 1; int b = 2; swap(a, b); System.out.println(a + " " + b + " "); } 

public static void swap(int a, int b) { a = b; b = a; System.out.print(a + " " + b + " "); }} 

a) 1 2 2 1b) 2 2 1 2c) 2 1 2 1d) 2 2 2 2e) 1 2 2 2

Section A: Question 10String str = "This is a string.";

String newStr = str.substring(str.indexOf('s'), str.indexOf('g'));

System.out.println(newStr);

a) s is a strin

b) s is a string

c) strin

d) string

Section A: Question 10String str = "This is a string.";

String newStr = str.substring(str.indexOf('s'), str.indexOf('g'));

System.out.println(newStr);

a) s is a strin

b) s is a string

c) strin

d) string

Section B: Question 1 For each expression in the left-hand column, indicate its value in the right-hand column.

a. 10 + 3 * 5 / 3

b. 2.1 * 3.141 * (3 / 12) + 1.5

c. 75 % 10 + 3 % 10 – 12 % 3

d. 2 * 3 + “.” + (8 + 4) + 3 * 3

e. 982 / 10 / 10 / 2.0 * 2 + 21 / 5

f. 9768 % 10 % 10

g. 3 + 2 + “+” + 3 + 2

Section B: Question 1a10 + 3 * 5 / 3

10 + 3 * 5 / 3 10 + 15 / 3

10 + 15 / 3 10 + 5

10 + 5 15

Section B: Question 1b2.1 * 3.141 * (3 / 12) + 1.5

2.1 * 3.141 * (3 / 12) + 1.5 2.1 * 3.141 * 0 + 1.5

2.1 * 3.141 * 0 + 1.5 6.5961 * 0 + 1.5

6.5961 * 0 + 1.5 0 + 1.5

0 + 1.5 1.5

Section B: Question 1c75 % 10 + 3 % 10 – 12 % 3

75 % 10 + 3 % 10 – 12 % 3 5 + 3 % 10 – 12 % 3

5 + 3 % 10 – 12 % 3 5 + 3 – 12 % 3

5 + 3 – 12 % 3 5 + 3 – 0

5 + 3 – 0 8 – 0

8 – 0 8

Section B: Question 1d2 * 3 + “.” + (8 + 4) + 3 * 3

2 * 3 + “.” + (8 + 4) + 3 * 3 2 * 3 + “.” + 12 + 3 * 3

2 * 3 + “.” + 12 + 3 * 3 6 + “.” + 12 + 3 * 3

6 + “.” + 12 + 3 * 3 6 + “.” + 12 + 9

6 + “.” + 12 + 9 “6.” + 12 + 9

“6.” + 12 + 9 “6.12” + 9

“6.12” + 9 “6.129”

Section B: Question 1e982 / 10 / 10 / 2.0 * 2 + 21 / 5

982 / 10 / 10 / 2.0 * 2 + 21 / 5 98 / 10 / 2.0 * 2 + 21 / 5

98 / 10 / 2.0 * 2 + 21 / 5 9 / 2.0 * 2 + 21 / 5

9 / 2.0 * 2 + 21 / 5 4.5 * 2 + 21 / 5

4.5 * 2 + 21 / 5 9.0 + 21 / 5

9.0 + 21 / 5 9.0 + 4

9.0 + 4 13.0

Section B: Question 1f9768 % 10 % 10

9768 % 10 % 10 8 % 10

8 % 10 8

Section B: Question 1g3 + 2 + “+” + 3 + 2

3 + 2 + “+” + 3 + 2 5 + “+” + 3 + 2

5 + “+” + 3 + 2 “5+” 3 + 2

“5+” + 3 + 2 “5+3” + 2

“5+3” + 2 “5+32”

Section B: Question 2What is the output of the following program? (3 points for each correct line)

int one = 1;

int two = 2;

double three = 3.0;

one = theMethod(one, 2, three) +

theMethod(1, two, 1.0) +

theMethod(one, two, three);

System.out.println(“one + two is ” + (one + two));

Section B: Question 2theMethod(one, 2, three) theMethod(1, 2, 3.0)

public static int theMethod(int one, int two, double three) { int returnValue = (int) (one + two – three); System.out.println(returnValue); return returnValue;}

returnValue = (int) (one + two – three) = (int) (1 + 2 – 3.0) = 0

Print out 0 and return 0

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 0 +

theMethod(1, two, 1.0) +

theMethod(one, two, three);

System.out.println(“one + two is ” + (one + two));

output:0

Section B: Question 2theMethod(1, two, 1.0) theMethod(1, 2, 1.0)

public static int theMethod(int one, int two, double three) { int returnValue = (int) (one + two – three); System.out.println(returnValue); return returnValue;}

returnValue = (int) (one + two – three) = (int) (1 + 2 – 1.0) = 2

Print out 2 and return 2

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 0 +

2 +

theMethod(one, two, three);

System.out.println(“one + two is ” + (one + two));

output:02

Section B: Question 2theMethod(one, two, three) theMethod(1, 2, 3.0)

public static int theMethod(int one, int two, double three) { int returnValue = (int) (one + two – three); System.out.println(returnValue); return returnValue;}

returnValue = (int) (one + two – three) = (int) (1 + 2 – 3.0) = 0

Print out 0 and return 0

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 0 +

2 +

0;

System.out.println(“one + two is ” + (one + two));

output:020

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 2;

System.out.println(“one + two is ” + (one + two));

output:020

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 2;

System.out.println(“one + two is ” + (one + two));

output:020

Section B: Question 2int one = 1;

int two = 2;

double three = 3.0;

one = 2;

System.out.println(“one + two is ” + (2 + 2));

output:020one + two is 4

Section B: Question 3Write 2 methods which convert an English word to Pig Latin. Both methods take an input parameter which is a single English word. The method should return the Pig Latin word.

englishToPigLatin_ConsonantAtStart

• 1 point for using substring, 3 points for using it correctly

• 1 point for using charAt(), 3 points for using it correctly (substring(0, 1) also gets full credit)

• 1 point for concatenating “ay”, 3 points for concatenating correctly

• 1 point for using return statement to return a string, 3 points for returning the correct string

englishToPigLatin_VowelAtStart

• 1 point for concatenating “way”, 3 points for concatenating correctly

• 1 point for using return statement to return a string, 3 points for returning the correct string

Section B: Question 3 Write 2 methods which convert an English word to Pig Latin. Both methods take an input parameter which is a single English word. The method should return the Pig Latin word.

public static String englishToPigLatin_ConsonantAtStart(String englishWord) {

return englishWord.substring(1) + englishWord.charAt(0) + "ay";

}

public static String englishToPigLatin_VowelAtStart(String englishWord) {

return englishWord + "way";

}

Section B: Question 4 Write a program which read the size of a triangle from standard input and prints a “number triangle” of that size, as per the following examples:

Size of triangle: 3321211

Size of triangle: 5543214321321211

Section B: Question 4• 2 points for correctly declaring the Scanner• 1 point for trying to declare a Scanner

• 1 point for closing the Scanner

• 2 points for user prompt

• 2 points for reading an integer from the Scanner• 1 point for calling console.nextInt()• 1 point for assigning the value to an int

• 6 points for correct outer for loop• 2 points each for initialization, test, and update• 1 point if attempted but incorrect

• 6 points for correct inner for loop• 2 points each for initialization, test, and update• 1 point if attempted but incorrect

• 1 point for correct output

Section B: Question 4 public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Size of triangle: "); int size = console.nextInt(); triangle(size); console.close(); }

public static void triangle(int size) { for (int i = size; i > 0; i--) { for (int j = i; j > 0; j--) { System.out.print(j); } System.out.println(); } }

Just wondering…On a scale from 1 to 10 (1 being super duper easy, 10 being totally impossible), how difficult was the homework assigned on Monday?

Who has started the FracCalc checkpoint?

Who has finished the FracCalc checkpoint?

Homework• Read: BJP 4.2

• Self-checks: 4.2, 4.5, 4.6, 4.13, 4.14, 4.15, 4.16, 4.19

• Exercises: 4.3, 4.4

• FracCalc checkpoint 1 is due at the beginning of class on Friday•Worth 10% of your project grade• No make-up or partial credit• If you haven’t started, START TONIGHT!