CS118 cramming cheat sheet

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

What happens when we have an else/else if without curly braces?

  • Dangling else problem

  • else will default to the last if that came before it

    • Code may not execute if else was meant for the first if in the block, and several if’s came after it

2
New cards

What is the issue of going from longint and how can it be overcome?

  • long is more precise than int

  • Could lead to a lossy conversion where data is lost and might be less precise

  • Requires a change in return type or an explicit cast

3
New cards

Where can’t switch statements be used?

  • Where there are no discrete values present (e.g. case is an inequality or a range)

4
New cards

What is the process of dealing with a Q2/3-type programming problem?

  • Understand the prompt — identify nouns and verbs, and what concept they relate to

  • Split the task into several steps, input → output → calculation

  • Simplify using variables and arrows

  • Think about how they can be implemented in Java

5
New cards

How should we deal with fields implementing data structures in classes?

  • Declare the field in the class

  • Do not pass it in as a parameter in the constructor, but initialise it within the constructor block

class ClassName { 
 	List<E> list
	int p1;
	int p2;
	
	public ClassName(int p1, int p2) {
		this.p1 = p1;
		this.p2 = p2;
		this.list = new ArrayList<>();
	}
	// ...
}

6
New cards

How should abstract classes and methods be implemented?

  • Declare the method as abstract but do not implement it

  • Inside concrete class, implement the method using @Override notation (without calling it abstract)

7
New cards

How should checked and unchecked exception classes be handled?

  • Unchecked → exception is a subclass of RuntimeException

  • Checked → exception is a subclass of Exception

8
New cards

How should exception-throwing methods be declared?

public [ReturnType] method throws [ExceptionName](…) { … }

throws MUST be there if the method is throwing the exception

9
New cards

What is the format of a generic class?

  • Uses <T> to show that it is a generic class

class ClassName<T> { … }

10
New cards

What would the generic class look like if the generic type can only be subclasses of another type?

Using List<?> as an example:

class ClassName<T extends List<?>> { … }

11
New cards

How are interfaces implemented?

  • Interface only stores the method signature (no body)

  • @Override notation when we implement the methods defined in the interface

  • Included in the class:

public class [ClassName] implements [InterfaceName] { … }

12
New cards

When is @Override notation used?

  • Either when we implement a method from an interface, or we are overriding a method from a superclass