computer science

ginko1412
studyguidecs107.docx

STUDY GUIDE

CS107,

----------------------

*. HINT: the "assignment operator", the "=" sign, works like this:

it calculates the thing on its right side,

then it puts it into the variable on its left side.

Example:

int n = 10;

n = n*2 + 6;

This first puts a 10 into n.

Then, it calculates n*2+6, which is 10*2+6, which is 26,

then it puts the 26 into n.

------

NOTE:

for some of the "what will the program output" questions,

the questions will ask you to specify the output by selecting Scantron answers

corresponding to EACH outputted item.

(For example,

select the Scantron answer specifying the first item/number outputted,

select the Scantron answer specifying the second item/number outputted, etc.)

------

NOTE:

ALL "POTENTIAL" QUESTIONS ARE LISTED BELOW,

BUT ONLY A SUBSET OF THESE WILL ACTUALLY BE ASKED ON EXAM 2.

Exam questions are similar to,

but not necessarily identical to, the following items:

------

------

**************

************** PART 1: SOME CONTENT FROM THE EXAM#1 STUDY SHEET:

**************

*. Know the difference between System.out.println and System.out.print

*. Name the four "integer" data types: byte, short, int, long.

Name the two "real number" (floating point) data types: float, double.

*. Know how to declare a specified variable, //Example: int n;

and how to initialize a specified variable. //Example: n = 10;

Also, know how to declare and initialize using the single-line shortcut.

//Example: int n = 10;

*. Know the file names, file descriptions, and file contents,

for the files during a typical programming workflow:

Prog1.java --- source code file --- contains your source code (your program)

Prog1.class --- bytecode file --- contains your bytecode, which is

your program written in the machine language

of the hypothetical JVM CPU/computer.

*. Know the commands and steps for a typical programming workflow:

compile your source code, with: javac Prog1.java

run your program, by interpreting your bytecode, with: java Prog1

*. Know what software is the "javac" and "java" programs:

javac --- the "Java compiler"

--- turns your source code into a new "bytecode file"

(NOT into a new "executable file"!).

java --- the "Java interpreter"

--- runs your program, by turning your bytecode

into local machine language. This translation

of language occurs while your program is running.

*. Given some println and print statements,

specify what the program will output.

Example:

System.out.print("Hi");

System.out.print("Hello");

System.out.println("Welcome");

System.out.println("Greetings");

System.out.print("Bye");

ANSWER:

HiHelloWelcome

Greetings

Bye

*. Given a println with multiple "+" operators,

specify what the program will output.

Example:

System.out.println("The sum = " + 2+3);

System.out.println("The sum = " + (2+3));

ANSWER:

The sum = 23

The sum = 5

*. Given a println with a text phrase containing escape characters,

such as " or \, specify what the program will output.

Example: System.out.println("The \\\"thing\"\\");

Answer: the output is:

The \"thing"\

*. Naming rules, and naming customs: NOT on Exam 2.

*. Know the 3 "suffixes" for casting a data type without the cast operator:

L, F, D

*. Know the "arithmetic operators": + - * / %

EXAMPLE:

System.out.println(2*3); //6

System.out.println(11%3); //2

*. Know the precedence rules, a.k.a. "PEMDAS", for arithmetic operators.

Answer: parentheses, exponents,

* and / from left to right, + and - from left to right.

EXAMPLE:

System.out.println(2 + 3 * 100); //302

System.out.println( (2 + 3) * 100 ); //500

*. Know the ways to increase or decrease the value of a variable.

Examples:

n++;

n--;

n = n + 10;

n = n - 10;

n += 10;

n -= 10;

*. Given an expression or "mixed-mode expression",

specify what the computer will calculate as the answer.

EXAMPLES:

System.out.println(3 + 6); //9

System.out.println(3.0 + 6.0); //9.0

System.out.println(3 + 6.0); //9.0

System.out.println(3.0 + 6); //9.0

System.out.println(1/2); //0

System.out.println(1/2.0); //0.5

*. Given an assignment or "mixed-mode assignment" statement,

specify whether it is okay (and then specify what the assigned number is),

or whether it will crash with an error.

The statement might include a "cast".

EXAMPLES:

int k = 6.5; //error!

//can't put a double into an int (wide into narrow)

double w = 100; //w is set to 100.0

double xx = (int)6.5; //xx is set to 6.0 -- tricky!

float f = 6.5; //error!

//can't put a double into a float (wide into narrow)

float ff = 6.5F; //ff is set to float 6.5, not double 6.5

byte j = 10; //j is set to byte 10 --

//compiler breaks its own rules about wide into narrow --

//it allows the int into the byte

//because the 10 is in legal range for a byte

long n = 1000000000000L; //n is set to a long integer 1000000000000

long z = 1000000000000; //error!

//the number is treated as an int because no decimal,

//but it's out of range for an int, causing an error

*. Given some program code, fill in the blanks to complete the program,

to accomplish the specified requirements.

For example, output the odd numbers from 11 to 19.

EXAMPLES:

for (int i=11; i<=19; i=i+2) //or, slot2 as this: i<20 which is the same

{

System.out.println(i);

}

//OR:

for (int i=10; i<=18; i=i+2) //or, slot2 as this: i<19 which is the same

{

System.out.println(i+1);

}

//OR:

for (int i=1; i<=5; i++) //or, slot2 as this: i<6 which is the same

{

System.out.println(2*i + 9);

}

---

*. Given some lines of FOR loop code, specify what the code will output.

This will include basic FOR loops, and "weird FOR loops".

The "weird FOR loops" are asked,

to test whether you know the "3 steps" of a FOR loop.

SAMPLE:

for (int i=2; i<=12; i=i+2)

{

System.out.println( 2*i );

}

ANSWER:

4

8

12

16

20

24

SAMPLE:

for (int x=3 ; x<=6 ; x++)

{

int y = x + 3;

System.out.println("x=" + x + ", y=" + y);

}

ANSWER:

x=3, y=6

x=4, y=7

x=5, y=8

x=6, y=9

SAMPLE:

int x = 20;

int y = 8;

for (int z=10; y<=x-8; z++) //DON'T COMPUTE BEYOND THE 5TH OUTPUT

{

y = y + 12;

z = z + 2;

x = x + 20;

System.out.println(z);

}

ANSWER:

12

15

18

21

24

//etc......

//instructions say, don't compute beyond the 5th output

SAMPLE:

int x = 1;

int y = 30;

for (int z=10; x<y; z++)

{

z++;

System.out.println(z);

y = y - 6;

x = x + 2;

}

ANSWER:

11

13

15

17

SAMPLE:

for (int i=1; i>=3; i++) //caution!

{

System.out.println("Hello");

}

ANSWER:

[no output]

SAMPLE:

for (int i=1; i<2; i++) //caution!

{

System.out.println(i);

}

ANSWER:

1

---

*. Given some lines of NESTED FOR LOOP code,

specify what the code will output.

SAMPLE:

for (int x=2; x<5; x++)

{

for (int y=6; y>=x; y--)

{

System.out.println("x=" + x + ", y=" + y);

}

}

ANSWER:

x=2, y=6

x=2, y=5

x=2, y=4

x=2, y=3

x=2, y=2

x=3, y=6

x=3, y=5

x=3, y=4

x=3, y=3

x=4, y=6

x=4, y=5

x=4, y=4

---

*. Given some lines of IF code,

specify what the code will output.

SAMPLE:

int a = 10;

if (a == 2)

{

System.out.print("Red ");

}

else if (a == 10)

{

System.out.print("Green ");

}

else if (a == 10)

{

System.out.print("Blue ");

}

else

{

System.out.print("Yes ");

}

ANSWER:

Green

------

------

**************

************** PART 2: NEW CONTENT FOR EXAM#2:

**************

*. Given some lines of WHILE loop code,

specify what the code will output.

EXAMPLE:

int n = 1;

int k = 10;

while ( n < k+1 )

{

n = n + 3;

System.out.println(k);

}

OUTPUT:

10

10

10

10

EXAMPLE:

int n = 6;

while ( n > 10 )

{

System.out.println("Hello");

n++;

}

OUTPUT:

[no output]

EXAMPLE:

int y = 13;

int x = 20;

int k = 20;

while (y+3 < x+2)

{

x--;

k = 3 + 2*k;

y--;

System.out.println(y);

System.out.println(k-2);

y = y + 2;

}

OUTPUT:

12

41

13

87

14

179

---

*. Given some lines of DO WHILE loop code,

specify what the code will output.

EXAMPLE:

int n = 1;

int k = 10;

do

{

n = n + 3;

System.out.println(k);

}

while ( n < k+1 );

OUTPUT:

10

10

10

10

EXAMPLE:

int n = 6;

do

{

System.out.println("Hello");

n++;

}

while (n > 10);

OUTPUT:

Hello

EXAMPLE:

int z = 50;

int y = 25;

int x = 5;

do

{

z = z * 2;

y = y - 2 ;

z = z - 10;

System.out.println(z);

System.out.println(y);

x = x + 5;

}

while ( x+3 <= y+8 );

OUTPUT:

90

23

170

21

330

19

650

17

---

*. You can code any type of loop to act like any type of loop,

but know the customs/traditions for choosing loops:

FOR loop: when you know the number of repetitions

WHILE loop: when you don't know the number of repetitions,

but you want 0 or more repetitions

DO-WHILE loop: when you don't know the number of repetitions,

but you want 1 or more repetitions

*. Know how to test numbers (like int's) for equality:

if (x == y)

Know how to test numbers (like int's) for inequality:

if (x != y)

Know how to test Strings for equality:

if ( s.equals(t) )

Know how to test Strings for inequality:

if ( ! s.equals(t) )

*. Know that .equals is extremely strict:

"STOP" is NOT equal to "stop"

"STOP" is NOT equal to "STOP "

*. Exam2 will NOT include "toUpperCase" or "replace" or "contains".

---

*. Given some lines of NESTED IF code,

specify what the code will output.

EXAMPLE:

int a = 20;

if (a < 10)

{

if (a >= 18)

{ System.out.println("Part1"); }

else

{ System.out.println("Part2"); }

}

else if (a == 20)

{

if (a > 100)

{ System.out.println("Part10"); }

else

{ System.out.println("Part20"); }

}

else if (a == 20)

{

System.out.println("Part100");

}

else

{

System.out.println("Part200");

}

ANSWER:

Part20

EXAMPLE:

int x = 2;

int y = 1;

if (x < y)

{

if (x > y)

{

System.out.println("R");

}

else

{

System.out.println("W");

}

}

else

{

if (x < y)

{

System.out.println("100");

}

else

{

System.out.println("200");

}

}

OUTPUT:

200

---

*. LOGICAL OPERATORS

SAMPLE:

int age = 21;

if (age==30 || age==20 || age==21)

{

System.out.println("Greetings");

}

else

{

System.out.println("Hello");

}

OUTPUT:

Greetings

SAMPLE:

int age = 21;

if (age==20 && age==21)

{

System.out.println("Greetings");

}

else

{

System.out.println("Hello");

}

OUTPUT:

Hello

SAMPLE:

int n = 80;

if (n>=0 && n<=100)

{

System.out.println("Legit score");

}

else

{

System.out.println("Error, bogus score");

}

OUTPUT:

Legit score

SAMPLE:

int n = 110;

if (n>=0 && n<=100)

{

System.out.println("Legit score");

}

else

{

System.out.println("Error, bogus score");

}

OUTPUT:

Error, bogus score

---

*. Know the "logical operators" and their "truth tables":

&& --- lazy/fast AND ('short circuit' AND)

|| --- lazy/fast OR ('short circuit' OR)

& --- AND

| --- OR

^ --- XOR, 'exclusive OR'

! --- NOT

TRUTH TABLES:

T and T -----> T

T and F -----> F

F and T -----> F

F and F -----> F

T or T -----> T

T or F -----> T

F or T -----> T

F or F -----> F

T xor T -----> F

T xor F -----> T

F xor T -----> T

F xor F -----> F

not T -----> F

not F -----> T

EXAMPLES:

System.out.println(true ^ true); //false

System.out.println(true && true); //true

System.out.println( ! false ); //true

---

*. Give some lines of SWITCH CASE code,

specify what the code will output.

EXAMPLE:

int k = 10;

switch (k)

{

case 0:

case 1: System.out.println("Hi");

break;

case 2: System.out.println("Hello");

break;

default: System.out.println("Welcome");

break;

}

OUTPUT:

Welcome

EXAMPLE:

int k = 10;

switch (k)

{

case 0:

case 1: System.out.println("Hi");

break;

case 2: System.out.println("Hello");

break;

}

OUTPUT:

[no output]

EXAMPLE:

int k = 2;

switch (k)

{

case 0:

case 1: System.out.println("Hi");

break;

case 2: System.out.println("Hello");

break;

default: System.out.println("Welcome");

break;

}

OUTPUT:

Hello

EXAMPLE:

int k = 2;

switch (k)

{

case 0:

case 1: System.out.println("Hi");

case 2: System.out.println("Hello");

default: System.out.println("Welcome");

}

OUTPUT:

Hello

Welcome

EXAMPLE:

int k = 10;

switch (k)

{

case 0: System.out.println("Hi");

case 1: System.out.println("Hello");

case 10: System.out.println("Welcome");

case 11: System.out.println("Bye");

}

OUTPUT:

Welcome

Bye

---

*. ARRAYS

Fill-in-the-blanks, to complete this "array sum, with a FOR loop" program:

public class Prog3

{

public static void main(String args[])

{

int[] a = {10, 30, 20, 60, 100, 10, 10};

int sum = 0;

for (int i=0; i<=a.length-1; i++) ////////or, slot2 as this: i<a.length

{

sum = sum + a[i];

}

System.out.println(sum);

}

}

---

*. ARRAYS

Know what these printlns will output:

int[] a = {10, 20, 30};

System.out.println(a); //RAM hash gibberish

System.out.println(java.util.Arrays.toString(a)); // [10, 20, 30]

//or, if you import util, you can shortcut that mess, to this:

// System.out.println(Arrays.toString(a));

---

*. ARRAYS

Specify what some code will output:

EXAMPLE:

int[] a = {10, 20, 30};

for (int i=0; i<=2; i++) //or, slot2 as this: i<=a.length-1

{

System.out.println(a[i]);

}

OUTPUT:

10

20

30

EXAMPLE:

int[] a = {10, 20, 30};

for (int i=0; i<3; i++) //or, slot2 as this: i<a.length

{

System.out.println(a[i]);

}

OUTPUT:

10

20

30

EXAMPLE:

int[] a = {10, 20, 30};

for (int x : a)

{

System.out.println(x);

}

OUTPUT:

10

20

30

---

*. ARRAYS

Specify the results of some printlns:

int[] arr = {10, 20, 30};

System.out.println(arr.length); //3

System.out.println(arr[0]); //10

System.out.println(arr[1]); //20

System.out.println(arr[2]); //30

System.out.println(arr[3]); //error! Array index out of bounds.

---

*. ARRAYS

Specify the results of some printlns:

int[] arr = new int[10];

System.out.println( a[0] ); //0

System.out.println( a[1] ); //0

a[0] = 100;

a[1] = 2*a[0] + 100;

System.out.println(a[0]); //100

System.out.println(a[1]); //300

//assume importing util, at top of program file

System.out.println(Arrays.toString(arr));

// [100, 300, 0, 0, 0, 0, 0, 0, 0, 0]

---

*. ARRAYS:

Know how to declare and initialize an array to 0's, in a single line.

Example: int[] a = new int[10];

Know how to declare and initialize an array,

using the single-line braces shortcut.

Example: int[] a = {10, 20, 30};

NOT on Exam#2: declare and initialize an array, using the

two-line braces shortcut.

Know: how to access an array element,

that is, how to get/access the number stored at a particular

array position/index.

Answer: arrayname[arrayindex], such as a[2] or a[i], etc.

Know: how to set an array element,

that is, how to put/store a number into an array position/index.

Answer: arrayname[arrayindex] = info;

such as a[2] = 100;

or a[9] = a[2]*6 + 10;

-------------------------------------------------------------------------------

-------------------------------------------------------------------------------