exam review Flashcards

1
Q

What type of browser is required to run applets?

A

Applets run in a Java-enabled web browser.

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

How does Java treat case sensitivity?

A

Java is case sensitive; identifiers like variable, method, and class names must be written with consistent letter casing.

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

How is Java generally categorized in terms of execution?

A

Java is known as an interpreted language because it compiles to byte code and then is interpreted (or just-in-time compiled) by the JVM.

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

What does the term “statement” mean in Java programming?

A

A statement is a coding instruction that can span multiple lines and ends with a semicolon.

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

How is data stored in memory, particularly for variables?

A

Data is stored in memory, where a variable name represents a memory location (e.g., int var1 = 0; stores 0 in a memory address and var1 represents that address). Declarations (e.g., int var;) and assignments (e.g., var = 0;) are separate operations.

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

What is the file extension for Java source code and what does it contain?

A

Java source code uses the .java extension and contains one or more classes. Only one public class is allowed, and it must have the same name as the file.

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

What file is produced after compiling Java source code and what is checked at that stage?

A

The compiler produces .class files (byte code) and checks for syntax errors during compilation.

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

How does the JVM execute a Java program?

A

The JVM interprets the .class (byte code) file and executes the code by emulating a microprocessor since the CPU cannot run byte code directly.

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

What is mandatory for every Java application?

A

Every Java application must have a main method, although not every class needs one.

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

What are three important Java keywords and their roles?

A

public (accessible to the JVM and other classes), static (belongs to the class rather than an instance), and void (indicates no return value).

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

What are the main characters/symbols used in Java and what are they for?

A
  • // for single line comments
  • /* … */ for block comments
  • /** … */ for Javadoc comments
  • () for method headers and conditionals
  • {} to enclose blocks (classes, methods, loops)
  • ”” for strings and ‘’ for chars
  • ; to end a statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some essential escape sequences in Java?

A
  • \t for a tab
  • \b for backspace
  • \r for carriage return
  • \ to print a backslash
  • ' or " to print quotes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the standard Java library provide?

A

It is a collection of pre-built classes accessed through the Java API. For example, the System class provides methods like System.out.println() and System.out.printf(), and the Math class provides constants (PI, E) and methods for trigonometry, exponentiation, rounding, etc.

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

How do you import and use the Scanner and Random classes from java.util?

A

Use Scanner to read input: e.g., Scanner keyboard = new Scanner(System.in); and call methods like .next() or .nextLine() (with char ch = s1.charAt(0); for a character). Use Random to generate numbers: e.g., Random rand = new Random(); int num = rand.nextInt(n); where n is the upper bound (non-inclusive).

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

Can string literals span multiple lines in Java?

A

No, string literals cannot span lines unless you break them up and concatenate multiple strings.

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

What are identifiers and what are the rules for them?

A

Identifiers (names for classes, methods, variables) may contain letters, numbers, underscores, and dollar signs, must not start with a number, are case sensitive, and cannot include spaces.

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

What are the primitive data types in Java?

A

Primitive data types include byte, short, int, long (1/2/4/8 bytes respectively), float and double (4/8 bytes with about 7/15 decimal precision), boolean, and char.

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

What is a special rule for assigning literals to long and float types?

A

For a long literal, you must append l or L (e.g., 100L). For a float literal, append f or F (e.g., 23.5f) since decimal literals default to double.

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

How are characters stored in Java?

A

Characters (char) are stored as numbers (Unicode), using 2 bytes per char. The first 256 characters are compatible with ASCII.

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

What are the three variable types based on their scope in Java?

A
  • Local variables: declared inside a method/block (no default value).
  • Instance variables: non-static variables declared in a class (unique to each object).
  • Class variables: declared as static, shared across all instances of the class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What happens during integer division in Java?

A

Integer division truncates decimals. For example, 1/2 evaluates to 0; to get a floating-point result, you must cast one operand to a float/double or use a literal with a decimal.

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

What is casting and how does it differ between implicit and explicit casting?

A

Implicit casting (widening conversion) automatically converts to a larger type (e.g., int to long). Explicit casting (narrowing conversion) converts to a smaller type and may cause data loss (e.g., casting a double to an int truncates the decimal portion).

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

How can you convert between strings and numbers in Java?

A
  • To convert a string to a number, use methods like Integer.parseInt(string).
  • To convert a number to a string, you can concatenate it with an empty string (e.g., number + “”).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How are objects referenced compared to primitive data types?

A

Variables that reference objects store the memory address of the object, whereas primitives hold the actual value.

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

How can you write a one-line conditional statement in Java?

A

You can write it without curly braces, e.g., if (x > y) y = x;. The if-statement ends at the first semicolon after the condition if no curly braces are present.

26
Q

What is the conditional operator in Java and how does it work?

A

The conditional (ternary) operator is written as BoolExpression ? Value1 : Value2. If the boolean expression is true, it returns Value1; otherwise, Value2.

27
Q

What is the operator precedence order in Java for common operators?

A

The order is: logical NOT (!), then multiplication/division/modulus (* / %), followed by addition/subtraction (+ -), relational, equivalence relational, logical AND, logical OR, and finally assignment (including combined assignments). Parentheses can alter the natural precedence.

28
Q

What is the syntax of a basic switch statement in Java?

A

A switch statement checks a test condition and has case labels:
switch(testCondition) {<br></br> case condition:<br></br> // statements<br></br> break;<br></br> …<br></br> default:<br></br> // default statements<br></br>}
Test conditions can be of type char, byte, short, int, or String.

29
Q

What new features related to switch statements were introduced in Java 12?

A

Java 12 allows multiple case constants for a single case (e.g., case 1,3,5,7,9:) and an arrow syntax (case condition -> statement;) that can include multiple statements in a block. The new switch can also be used as an expression using yield to return a value.

30
Q

What are the common loop structures in Java and what are their characteristics?

A
  • while loop: pretest loop executing 0 or more times (commonly used for input validation).
  • do-while loop: post-test loop executing at least once.
  • for loop: includes initialization, condition, and update sections; can be used with multiple variables and even with empty sections (for (;;) for an infinite loop).
  • Enhanced for loop: used for iterating over arrays or collections, e.g., for (dataType item : array) { … }.
31
Q

What is a sentinel value in programming?

A

A sentinel value marks the end of input data, such as EOF or a specific value (e.g., -1) that signals termination of data input.

32
Q

What are the effects of using continue and break in loops?

A
  • continue skips the current iteration and moves to the next one.
  • break exits the loop entirely. (Note: Overuse can make code harder to read and debug.)
33
Q

How are arrays declared and what are some key properties of arrays in Java?

A

Arrays are objects that hold elements of a single type with a fixed size once created. They are declared as either int[] arr = new int[size]; or int arr[] = new int[size];. Arrays always have indices starting at 0, and they have a length field for size. Multi-dimensional arrays (jagged arrays and matrices) are also supported.

34
Q

What is the purpose of a vararg method?

A

A vararg method accepts any number of arguments as an array. For example: public static int sum(int… numbers) can be called with different numbers of int arguments.

35
Q

How are command line arguments passed to a Java program?

A

When the main method takes parameters (e.g., public static void main(String[] args)), command line arguments are passed as an array of strings. For example, running java CommandLineExample hello world passes “hello” and “world” as elements in args.

36
Q

What are the key components of a method header in Java?

A

A method header includes the modifiers (e.g., public, static), the return type (e.g., void), the method name, and an argument list enclosed in parentheses.

37
Q

What is a method signature and how does Java use it?

A

The method signature consists of the method name and its parameter list. It is used by Java for binding—the process of matching a method call to the correct method definition (overloading based on parameter types, e.g., sum(int, int) vs. sum(double, double)).

38
Q

How are primitive data types and objects passed to methods?

A

Primitive types are passed by value (a copy is made) so changes do not affect the original. Object references are passed by value as well, but changes to the object’s data affect the original object (keeping in mind that strings are immutable).

39
Q

What are default methods in classes and how can they be overridden?

A

Some classes provide default methods like toString(); these may be called implicitly or explicitly. They can be overridden using the @Override annotation.

40
Q

What defines a class in Java and what are its components?

A

A class is a blueprint for creating objects. It defines data (fields) and behavior (methods). In UML, a class diagram is divided into three sections: the class name, fields (with access specifiers), and methods (with signatures).

41
Q

What are instance fields/methods and how do they differ from static ones?

A

Instance members (fields/methods) belong to a specific object and are accessed via that object, while static members belong to the class itself and are shared across all instances.

42
Q

What is a constructor and what are its rules in Java?

A

A constructor is a special method called when a new object is created. It must have the same name as the class, has no return type, can be overloaded, and if none is provided, a default constructor is automatically created (setting numeric fields to 0, booleans to false, and object references to null).

43
Q

What is shadowing in Java and how can it be resolved?

A

Shadowing occurs when a local variable in a method has the same name as an instance field, thereby hiding the instance field. This can be resolved by using this.fieldName to refer to the instance field.

44
Q

How do shallow and deep copies differ in Java?

A

A shallow copy creates a new reference to the same object (both variables point to the same memory), while a deep copy creates an entirely new object with copies of the original object’s data, often done with a copy constructor.

45
Q

What is aggregation in object-oriented programming?

A

Aggregation is a “has-a” relationship where one class contains references to objects of other classes (e.g., a course has an instructor and a textbook).

46
Q

Why should you avoid returning references to private data elements?

A

Returning direct references to private data can allow external modification, so it is safer to return a copy of the data to preserve encapsulation.

47
Q

How does Java handle garbage collection?

A

Java handles garbage collection automatically by reclaiming memory for objects that are no longer referenced (e.g., after setting an object reference to null).

48
Q

What is an ArrayList and how does it differ from an array?

A

An ArrayList is a resizable array provided by the Java Collections Framework. It automatically expands or shrinks, and offers methods like add, set, get, and remove. Its default capacity is 10 if not specified.

49
Q

What are exceptions in Java, and what distinguishes checked from unchecked exceptions?

A

Exceptions are abnormal events during program execution. Checked exceptions (like file I/O issues) must be handled or declared, whereas unchecked exceptions (runtime exceptions and errors) do not require explicit handling, though they can occur (e.g., division by zero).

50
Q

What is the role of the call stack in exception handling?

A

The call stack is a record of the active subroutines in a program. When an exception occurs, the stack trace lists the method calls leading to the exception, helping diagnose where the error happened.

51
Q

What is a Java collection and what are its main types?

A

A collection is a container that holds groups of objects. The main types include Lists (ordered sequence), Sets (unique elements), and Maps (key-value pairs).

52
Q

How do ArrayList, LinkedList, Vector, and Stack differ?

A
  • ArrayList: Fast random access via index; less efficient insertions/deletions in the middle.
  • LinkedList: Efficient insertions/deletions at the ends; slower random access.
  • Vector: Similar to ArrayList but with synchronized methods (thread-safe).
  • Stack: Inherits from Vector; follows LIFO (last in, first out) with methods such as push and pop.
53
Q

What is the Queue interface and what are its common methods?

A

A Queue is a FIFO (first in, first out) collection. Common methods include offer (add), poll (remove), remove, peek (examine first element), and element.

54
Q

How are maps used in Java and what are the differences between HashMap and TreeMap?

A

Maps store key-value pairs. A HashMap is optimized for efficient lookup, insertion, and deletion, while a TreeMap maintains the keys in sorted order, making traversal in order easier.

55
Q

What is linear search, and what is its time complexity?

A

Linear search compares each element from index 0 to n. Its time complexity is O(n).

56
Q

How does binary search work and what is its requirement?

A

Binary search requires the array to be sorted in ascending order. It checks the middle element, then continues searching in either the left or right half. Its time complexity is O(log n).

57
Q

What is selection sort and how does it generally work?

A

Selection sort repeatedly selects the smallest value from the unsorted portion and moves it to the beginning, building a sorted portion of the array.

58
Q

What does Big O notation represent and why is it important?

A

Big O notation describes the order of magnitude for algorithmic growth rates, focusing on worst-case scenarios. It ignores constant factors and non-dominant terms, allowing us to compare algorithm efficiency as input size increases.

59
Q

What is the typical hierarchy of growth rates in Big O notation?

A

The common hierarchy is: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(n³) < O(2^n).

60
Q

What are examples of time complexities for common algorithms?

A
  • O(log n): Binary search, GCD
  • O(n): Linear search
  • O(n log n): Merge sort
  • O(n²): Selection sort, insertion sort
  • O(2^n): Recursive solutions like Towers of Hanoi or certain Fibonacci implementations