Coding Resources

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/67

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:37 AM on 8/25/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

68 Terms

1
New cards

What is the difference between an int and a double?

  • int stores whole numbers: -3, 0, 42

  • double stores decimal/floating-point numbers: 3.14, -0.5, 42.0


2
New cards

What happens when you divide two ints in Java?

You get integer division: the decimal portion is discarded.

3
New cards

What happens when at least one operand in a division is a double?

double x = 7 / 2.0;

x is 3.5.

4
New cards

What is the result of 5 / 2?

2, because both operands are ints

5
New cards

What is the result of 5.0 / 2?

2.5

6
New cards

What does casting do?

Casting explicitly converts a value from one data type to another.

Ex:

double x = 7.8;

int y = (int)x;

y becomes 7.

7
New cards

Does casting a double to an int round the number?

No. Casting truncates the decimal portion.

(int) 3.99 // 3

(int) -3.99 // -3

Think cut off the decimal, not round.

8
New cards

How can you force integer values to produce decimal division?

int a = 5;

int b = 2;

double result = (double)a / b;

Result: 2.5. If you cast after the devision it would be too late.

9
New cards

Question: What is the difference between these?

(double)(5 / 2)

(double)5 / 2

First performs integer division then casts 2.0. The second converts 5 to a double then performs decimal devision, getting 2.5

10
New cards

What is a variable?

A named location that stores a value

11
New cards

What is the difference between a char and a String?

A char represents one character: char letter = ‘A’;

A String represents a sequence of characters: String word=”Apples”;

12
New cards

What is a parameter?

A variable listed in a method's header that receives a value when the method is called.

public static int square(int x)

x is a parameter here

13
New cards

What is an argument?

The actual value you pass into a method when calling it.

square(5);

5 is the argument, x is the parameter

14
New cards

What is a method's return type?

public static double half(int x)

The return type is double.

15
New cards

What does void mean as a return type?

The method does not return a value.

public static void printHello()

16
New cards

If a method has return type int, what must it ultimately do?

It must return an int.

public static int add(int a, int b) {

return a + b;

}

17
New cards

What is the difference between return and System.out.println()?

return sends a value back to the code that called the method.

return x * 2;

System.out.println() displays something on the screen.

System.out.println(x * 2);

18
New cards

What does Math.abs() do?

Returns the absolute value.

19
New cards

What does Math.pow() do?

Math.pow(2, 3)

equals 8

20
New cards

What does Math.sqrt() do?

Math.sqrt(25)

Equals 5

21
New cards

What does Math.max(a, b) do?

Returns the larger of the two values.

Math.max(7, 12)

Equals 12

22
New cards

What does Math.min(a, b) do?

Returns the smaller of the two values.

Math.min(7, 12)

Equals 7

23
New cards

What does Math.random() return?

A random double greater than or equal to 0.0 and less than 1.0.

Possible values include 0,0, 0,372, 0,9999, but never 1.0

24
New cards

How can you generate a random integer from 0 through 9?

(int)(Math.random() * 10)

Results are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

25
New cards

How can you generate a random integer from 1 through 10?

(int)(Math.random() * 10) + 1

26
New cards

What does String.length() return?

String word = "Hello";

word.length();

Equals 5

27
New cards

What index does the first character of a String have?

Index 0

String word = "Hello";

Character H e l l o

Index 0 1 2 3 4

28
New cards

What is the index of the last character of a String?

length() - 1

For Hello,

5 - 1 = 4

29
New cards

What does charAt() do?

Returns the character at a particular index.

String word = "Hello";

word.charAt(1);

Equals ‘e’

30
New cards

What happens if you use an invalid String index?

Java throws a StringIndexOutOfBoundsException.

word.charAt(5) is invalid because valid indexes are 0-4

31
New cards

What does substring() do?

Returns part of a String.

String word = "Hello";

word.substring(1, 4);

Equals “ell”

32
New cards

Is the ending index of substring(start, end) included?

No.

"Hello".substring(1, 4)

takes indexes

1, 2, 3

but not 4

33
New cards

What does substring(start) do?

Returns everything from start through the end of the String.

"Hello".substring(2)

"llo"

34
New cards

What does indexOf() return?

The index where a character or substring first occurs.

"Hello".indexOf("l")

2

35
New cards

What does indexOf() return if it can't find what you're looking for?

-1

Example:

"Hello".indexOf("z")

-1

36
New cards

What is the difference between indexOf() and lastIndexOf()?

indexOf() finds the first occurrence.

lastIndexOf() finds the last occurrence.

"banana".indexOf("a")      // 1
"banana".lastIndexOf("a")  // 5


37
New cards

How do you compare two Strings for equality in Java?

Use .equals().

name.equals("Bob")

Do not normally use:

name == "Bob"

for String-content comparison.

38
New cards

What does "Hello".equals("Hello") return?

true

39
New cards

What does "Hello".equals("hello") return?

false

String comparison with .equals() is case-sensitive.

40
New cards

How do you compare Strings without caring about capitalization?

Use:

.equalsIgnoreCase()

Example:

"Hello".equalsIgnoreCase("hello")

true

41
New cards

What does toUpperCase() do?

Returns a new String with letters converted to uppercase.

"hello".toUpperCase()

"HELLO"

42
New cards

What does toLowerCase() do?

"HELLO".toLowerCase()

"hello"

43
New cards

Are Java Strings mutable?

No. Strings are immutable.

Methods such as:

toUpperCase()
substring()

don't modify the original String. They return a new String.

44
New cards

What happens here?

String word = "hello";
word.toUpperCase();

System.out.println(word);

hello

The original String wasn't changed.

To save the result:

word = word.toUpperCase();


45
New cards

What does the + operator do with Strings?

"Hello " + "world"

"Hello world"

46
New cards

What happens when a String is added to a number?

Java converts the number to a String and concatenates.

"Age: " + 17

"Age: 17"

47
New cards

What is the result of this?

System.out.println(2 + 3 + "hello");

5hello

Java evaluates from left to right:

2 + 3 → 5
5 + "hello" → "5hello"


48
New cards

What is the result of this?

System.out.println("hello" + 2 + 3);

hello23

Once Java encounters the String, the remaining + operations are concatenation.

49
New cards

What does the % operator do?

It gives the remainder after division.

7 % 3

1

50
New cards

What is 10 % 2?

0

Because 10 divides evenly by 2.

51
New cards

How can % be used to determine whether a number is even?

number % 2 == 0

If true, the number is even.

52
New cards

What does 17 % 10 equal?

7

The remainder after dividing 17 by 10 is 7.

This is useful for extracting the last digit of a number.

53
New cards

What is the result of -7 / 2 using integer division?

-3

Java truncates the fractional part toward zero.

54
New cards

What is the result of (int) -3.9?

-3

Casting truncates toward zero; it does not round down to -4.

55
New cards

What is the difference between widening and narrowing conversions?

Widening: converting to a type that can represent a broader range, such as:

int → double

This generally happens automatically.

Narrowing: converting to a type with less range/precision, such as:

double → int

This generally requires an explicit cast.

56
New cards

What happens here?

int x = 5;
double y = x;


This is valid.

x is automatically converted to:

5.0

because an int can be safely represented as a double.

57
New cards

What happens here?

double x = 5.7;
int y = x;


This produces a compile-time error.

You need an explicit cast:

int y = (int)x;

Then y becomes 5.

58
New cards

What is the output?

int x = 9;
int y = 4;
double result = x / y;


2.0

Why? x / y happens first using integer division → 2. Then 2 is converted to double2.0.

59
New cards

What is the output?

int x = 9;
int y = 4;
double result = (double)x / y;

2.25

Casting x to a double forces floating-point division.

60
New cards

What is the output?

System.out.println(10 / 4);
System.out.println(10 / 4.0);

2
2.5

Same numbers, different data types.

61
New cards

What is the output?

String s = "Computer";
System.out.println(s.substring(2, 6));

mput

Indexes 2, 3, 4, 5 are included; index 6 is excluded.

62
New cards

What is the output?

String s = "banana";
System.out.println(s.indexOf("na"));


2

The first "na" starts at index 2.

63
New cards

What is the output?

String s = "banana";
System.out.println(s.indexOf("z"));

-1

The substring wasn't found.

64
New cards

What is the output?

String s = "Java";
System.out.println(s.charAt(s.length() - 1));


a

length() is 4, so the last index is 4 - 1 = 3.

65
New cards

What is the output?

String s = "Java";
System.out.println(s.substring(1));


ava

Starting at index 1 means everything from the second character to the end.

66
New cards

What is the output?

System.out.println((int)(Math.random() * 6));


One of:

0, 1, 2, 3, 4, 5

Never 6.

67
New cards

What is the biggest conceptual thing to remember about this unit?

Always track the data type and the order of operations.

Especially watch for:

int / int

vs.

double / int

and:

(double)(a / b)

vs.

(double)a / b


68
New cards

Remember:

  • String indexes start at 0.

  • substring's ending index is exclusive.

  • indexOf returns -1 when nothing is found.

  • Use .equals() for String-content comparison.

  • Strings are immutable.

  • Casting double → int truncates; it does not round.

  • Math.pow() and Math.sqrt() return double.

  • Math.random() is from 0.0 up to but not including 1.0.