Chapter 2 - In person Class Flashcards

1
Q

What’s the object in System.out.println (“Whatever”)?
What’s the method?
What’s the parameter?

A

system.out

println is the method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s the difference between Primitive data (for python and java).

A

Python figures it out for you, in java you have to be specific.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many types of primitive data are there? (can they be negative)

A

There are four integers. Two of them float, double. One of them are char, and the other boolean (yes).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why do computers approximate? How can that be a problem.

A

Because computers are limited. This can cause an issue when you are calculating total cost that need decimals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you add 1 to an integer type of 2147483647?

A

It rolls over and gives you -2147483648. So you went from billionaire to owing 2 billion. This is why it’s so important to know the type of data the integer is saved in. Compare that the cost of using more bits with type long.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between using ‘ and “ in Java? What language did Java come from?

A

You use single quotation mark for characters, where the uppercase and lowercase are different. Strings in Java use double quotation marks.

Java came from C.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When creating a variable in java do you have to specify the data type (different than python)?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s a literal? Give an example of a string literal.

A

It’s simply a value. “Hello” is a string literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is an expression a program statement?

A

No, they cannot exist by themselves. They are part of statements that end with a semi-colon ( ; ).

“Hello” + “World” is an expression but not a statement. String s = “Hello” + “World”; is a statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the %, a modulus (mod) operator? Why is it important?

A

It calculates the remainder. It’s important when we’re counting lists and telling the program to go back to the beginning after Remainder hits 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What part of this is the expression?

result = total + count / max - offset;

A

Everything to the right of result is the expression of the statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create a constant in Java? What is the style used for a CONSTANT?

A

add a final in front of it. final int var1 = 1;

The style is to capitalize it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do you need to know about data conversion? For example in one we may want to treat an integer as a floating point. (Java is a strongly typed language).

A

The data conversions do not change the type of variable. They do not affect the original variable or the original data value. You must be careful not to lose data value when going from wider to narrower type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the decrement operators ++ or –? Where will you see this all the time? It’s very common to use i for the outer loop, j for the next inner, and k for the next… and so forth.

A

count = 4. count++; is not the same thing as count = count +1; Count ++; is faster and adds only 1.
count++ is not the same as ++count.

total = count ++; Total becomes 4 first and then count++ becomes 5 after.

total = ++count; both Total and Count become 5.

We see ++ in loops all the time. for (int i = 0; i< 10 , i++)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are some assignment operators like +=? i.e. num += count;

Why is it important to learn this?

A

num += count; is equivalent to num = num + count.

This is because most of your job will not to start from scracth; but most likely fix other’s “crappy” coding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are narrowing and widening conversions? When can it happen automatically?

A

It can happen automatically when widening but not when narrowing.

17
Q

What are the three types of conversions? What is SOP?

A

1- assignment conversion.
2- promotion.
3- casting. (i.e. type casing when you place a ) in front of the int). when widening it’s not typecasting. Typecasting is forcing for a type to narrow.

SOP - system output public.

18
Q

What is the difference between using = sign and == sign?

A

= sign is an assignment. == is used to compare true or false.

19
Q

Why is casting the most powerful and most dangerous technique for conversion?

A

Casting is not going to check the types. It’s going to widen or narrow. put a parentheses in front of the value being converted.

Typecast has higher precedence. Use parentheses.

20
Q

How can you clarify (self-document) this statement, although it doesn’t change the result? result = (float) total / count;

A

result = ( (float) total ) / count;

int a = 97;
char c = ‘ ‘;
c = (char)a;

You cannot typecast a string literal like 1.

If char c = a, then c++ is equal to b.

21
Q

What is an interactive program? How do you add input?

A

The scanner class provides convenient methods for reading input. SYSTEM.IN is the keyboard!!!

22
Q

How do you create a scanner object? Where do you see them? DO WE NEED TO IMPORT?

A

All objects are created using the word NEW. Here we create a avg_input variable.

Scanner avg_input= new Scanner (System.in)
answer = avg_input.nextLine()

Yes, we need to IMPORT JAVA.UTIL.SCANNER;

23
Q

I will only use methods within the yellow boxes.

A

Scanner is on page 87.

import java.util Scanner;

String message;
Scanner scan;

scan = new Scanner (System.in);
System.out.println (“Enter a line of text”); // prompt the user
message = scan.nextLine();
scan.close();

24
Q

How can you use the scanner class to input integers or doubles.

A

double message1, message2;
Scanner scan;

System.out.println (“Enter two numbers separated by space and then hit enter”);

message1 = scan.nextInt();
message2 = scan.nextInt();

System.out.println (message1 + message2 + 5 + 7)

scan.close();

25
Q

What is the angry programmer (violent outburst programmer) rule?

A

Write your code and submit your work with the assumption it’s going to be read by an angry person who is prone to blowing up and coming after you with violence.

26
Q

What is a trace?

A

It’s to track the variable values as the program executes code line by line. It is something you can do manually or ask Eclipse to do it.