Java Basics Study List Flashcards

1
Q

What is source control?

A

Source control, also known as version control or revision control, is a system that manages and tracks changes to files and directories over time. It allows multiple developers to collaborate on a project by providing a centralized repository where changes can be recorded, tracked, and managed.

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

What is a Git repository?

A

a storage location where Git tracks and manages the history and versions of files for a project. It contains all the files, directories, and commit history associated with the project.

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

What does git add –all do?

A

used to stage all changes in the working directory for the next commit. It adds all modified, deleted, and new files to the staging area.

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

What does git commit -m “my comment” do?

A

used to create a new commit in the Git repository with a descriptive comment. It permanently saves the changes staged in the staging area and creates a new point in the project’s history.

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

What does git push do?

A

used to upload local commits to a remote repository. It sends the committed changes from the local branch to the corresponding branch on the remote repository, making them available to others.

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

What does git pull do?

A

used to fetch and merge changes from a remote repository into the current local branch. It updates the local branch with the latest changes from the remote repository.

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

Why would you create a branch in Git?

A

Allows developers to work on different features or bug fixes in isolation from the main branch. It enables parallel development and experimentation without affecting the main branch. Once the changes in the branch are complete and tested, they can be merged back into the main branch.

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

What does the git clone command do?

A

used to create a copy of a remote Git repository on a local machine. It initializes a new Git repository with the same commit history and files as the remote repository. Cloning allows developers to work with the project locally and make changes that can be synchronized with the remote repository.

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

What are the three steps in the life cycle of a java program?

A

Edit
Compile
Execute.

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

What is the difference between a compiler and an interpreter?

A

a compiler translates the entire program into machine code before execution, while an interpreter translates and executes the program line by line.

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

What is the syntax of the main method?

A

public static void main(String[] args) {
// Code goes here
}

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

Describe the benefits of using an IDE during programming.

A

features such as code completion, syntax highlighting, debugging tools, project management, version control integration, and automatic compilation, which enhance productivity and make the development process more efficient.

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

What is a syntax error?

A

Syntax errors occur when the code violates the language syntax rules and prevent the program from compiling.

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

List the differences between data and information.

A

Data refers to raw facts or values, while information is processed data that has meaning and relevance.

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

How do you write single-line and multi-line comments?

A

// This is a single line comment
/*
* This is a multi line comment
*/

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

What is wrong with the following code?
public class Hello {
String new = “My New House”;
String short = “I am short”;
String private = “I am a private person”;
int catch = 0;
double this = 1.0;
}

A

The variable names “new,” “short,” “private,” and “catch” are invalid because they are reserved keywords in Java.
The variable name “this” is also invalid because it is a reference to the current instance of the class.
Variable names should be meaningful and descriptive, not arbitrary.

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

Assign each value to a data type:
5
128
34,780
Bob
86.02
true
x

A

5: int
128: int
34,780: int
Bob: String
86.02: double
true: boolean
x: depends on the intended usage (e.g., int, double, String, etc.)

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

How would you keep the decimal value when doing the following operation?
int a = 10;
int b = 4;
float c = 0;

c = a / b;

A

int a = 10;
int b = 4;
float c = 0;

c = (float) a / b;

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

What data type is returned when reading from the console? What approach would you use to convert console data to another usable data type?

A

The data type returned when reading from the console is usually a String. To convert the console data to another usable data type, you can use type conversion methods such as Integer.parseInt() for integers, Double.parseDouble() for doubles, and so on.

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

What is a runtime error?

A

Runtime errors occur during program execution when an unexpected condition arises, such as division by zero or accessing an invalid memory location.

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

What is a logic error?

A

Logic errors occur when the program executes without any errors or exceptions, but produces incorrect or unexpected results due to flawed logic or algorithm.

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

Describe how you would debug/step through your code using your IDE?

A

set breakpoints at specific lines or methods in your code, run the program in debug mode, and then step through the code line by line to analyze the state and behavior of variables and objects at runtime.

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

What is the output of this method?
public static void main(String[] args) {
int a = 12;
int b = 13;
String C = “Java”;

System.out.println(a + b + C);
System.out.println(C + b + a); }
A

25Java
Java130

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

Analyze the following code and determine the output.
public static void main(String[] args) {
int a = 12;
int b = 13;
String C = “Java”;

if (a<=b) {
    System.out.println(a + " is less than " + b);
}
if (a<=b) {
    System.out.println(a + " is less than " + b);
} else {
    System.out.println("This is false.");
}
if (C.equals("JAVA")) {
    System.out.println("This is true");
} else {
    System.out.println("This is all uppercase!");
    if (C.equals("Java")) {
        System.out.println("This one is equal");
    }
} }
A

12 is less than 13
12 is less than 13
This is all uppercase!
This one is equal

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

Compare and contrast if statements with switch statements.

A

If statements allow conditional execution based on boolean expressions, while switch statements allow multi-branch selection based on the value of a variable or expression.
If statements can handle complex conditions and multiple comparisons, while switch statements are limited to equality comparisons.
If statements can use any boolean expression as a condition, while switch statements require a constant or an enum type for the controlling expression.

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

Write code to generate a random number between 1 and 6.

A

import java.util.Random;

public class RandomNumberExample {
public static void main(String[] args) {
Random random = new Random();
int randomNumber = random.nextInt(6) + 1;
System.out.println(randomNumber);
}
}

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

Evaluate and determine the output for the following code:

public static void main(String[] args) {
boolean a = true;
boolean b = false;
String c = “Java”;

if (a && b ) {
    System.out.println("Both a and b are true");
} else {
    System.out.println("Both a and b are not true");
}

if(a || b) {
    System.out.println("either a or b is true");
}

if (a && (b && c.equals("Java"))) {
    System.out.println("true");
} else {
    System.out.println("false");
} }
A

Both a and b are not true
either a or b is true
false

28
Q

Write code that generates the following output:
Counting down…

10
9
8
7
6
5
4
3
2
1
0

Blast off!

A

public class CountingDownExample {
public static void main(String[] args) {
System.out.println(“Counting down…\n”);
for (int i = 10; i >= 0; i–) {
System.out.println(i);
}
System.out.println(“\nBlast off!”);
}
}

29
Q

What is the difference between a while loop and a do/while loop?

A

a while loop checks the condition before each iteration, and if the condition is false initially, the loop is not executed at all. In contrast, a do/while loop executes the loop body at least once and then checks the condition after each iteration.

30
Q

Write code that generates the following output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6

A

public class NumberPatternExample {
public static void main(String[] args) {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + “ “);
}
System.out.println();
}
}
}

31
Q

Explain the DRY principle.

A

The DRY (Don’t Repeat Yourself) principle is a software development principle that promotes code reuse, maintainability, and reducing redundancy. It suggests that code should be organized in a way that avoids duplicating logic or information, and instead, common functionality should be abstracted into reusable components, functions, or methods.

32
Q

Create a method to calculate the area of a rectangle.

A

public class RectangleAreaExample {
public static void main(String[] args) {
double width = 5.0;
double height = 10.0;
double area = calculateRectangleArea(width, height);
System.out.println(“Area of the rectangle: “ + area);
}

public static double calculateRectangleArea(double width, double height) {
    return width * height;
} }
33
Q

Create a method to calculate the area of a triangle and return a value.

A

public class TriangleAreaExample {
public static void main(String[] args) {
double base = 5.0;
double height = 8.0;
double area = calculateTriangleArea(base, height);
System.out.println(“Area of the triangle: “ + area);
}

public static double calculateTriangleArea(double base, double height) {
    return 0.5 * base * height;
} }
34
Q

Explain the importance of scope.

A

Scope refers to the visibility and accessibility of variables, methods, and classes in different parts of a program. It determines where variables can be accessed and used.

35
Q

What does the final keyword mean when used with a method?

A

When the final keyword is used with a method, it means that the method cannot be overridden by any subclasses. It ensures that the method implementation remains unchanged in all subclasses.

36
Q

What does the term @override do to a method?

A

The @Override annotation is used to indicate that a method in a subclass is intended to override a method of the same signature in its superclass. It provides a way to ensure that the overriding is done correctly and helps in maintaining code clarity.

37
Q

How would you overload a method?

A

Method overloading refers to the concept of having multiple methods with the same name but different parameters within the same class. It allows a method to perform similar operations on different types of data or with different input combinations.

38
Q

What does the static keyword mean?

A

The static keyword in Java is used to declare class-level variables and methods. It means that the variable or method belongs to the class itself, rather than being associated with instances (objects) of the class.

39
Q

What is the syntax to declare a simple array? A two-dimensional array?

A

dataType[] arrayName;
dataType[][] arrayName;

40
Q

Write syntax to declare and initialize an array with the values (1,2,3,4,5).

A

int[] numbers = {1, 2, 3, 4, 5};
OR
int[] numbers;
numbers = new int[]{1, 2, 3, 4, 5};

41
Q

What occurred in the code if the code returns an ArrayIndexOutOfBoundException?

A

occurs when trying to access an array element with an index that is outside the valid range of indices for that array. It typically happens when the index is negative or exceeds the length of the array.

42
Q

What is the difference between an element and an index?

A

In an array, an element refers to a specific value stored at a particular index, while an index represents the position of an element within the array.

43
Q

What are the limitations of an array?

A

Arrays have a fixed size once declared and initialized, and the size cannot be changed dynamically.
Arrays can only store elements of the same data type.
Arrays may have memory constraints based on available memory resources.

44
Q

Is an array an object?

A

Yes

45
Q

Write code to print out the values of an array.

A

int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

46
Q

How does the garbage collector know when to return memory to the heap?

A

The garbage collector in Java automatically reclaims memory from objects that are no longer referenced by the program. It identifies objects that are no longer reachable and releases the memory occupied by those objects back to the heap.

47
Q

What is the stack

A

The stack is used for storing local variables, method calls, and references to objects. It follows a Last-In-First-Out (LIFO) structure and is automatically managed by the JVM.

48
Q

How is the import keyword used?

A

The import keyword is used in Java to access classes and packages from external libraries or other packages within the same project. It allows the programmer to use classes and their methods without fully qualifying the class names.

49
Q

What is a package?

A

A package in Java is a way to organize related classes and interfaces into a group. It provides a namespace for class names to avoid naming conflicts and facilitates modular development and code reuse.

50
Q

What is JavaDoc?

A

JavaDoc is a documentation generator tool provided by Oracle with the Java Development Kit (JDK). It allows developers to write comments in the source code using a specific syntax, which can be processed by the JavaDoc tool to generate API documentation in HTML format.

51
Q

Is the Java language pass by value or pass by reference?

A

Java is a pass-by-value language. When passing arguments to a method, the values of the variables are copied and passed to the method, rather than the actual variables or references.

52
Q

What does pass by value mean?

A

Pass by value means that the method receives a copy of the value stored in a variable, and any modifications made to the parameter within the method do not affect the original variable.

53
Q

What does pass by reference mean?

A

Pass by reference means that the method receives a reference or memory address of the variable, allowing direct access to the original variable. Any modifications made to the parameter within the method will affect the original variable.

54
Q

What is a NullPointerException?

A

A NullPointerException occurs when a program attempts to use a reference variable that is null (has no assigned object). It typically happens when trying to invoke a method or access a property of a null reference.

55
Q

What is the heap?

A

The heap is used for dynamic memory allocation, including objects and their instance variables. It follows a more complex allocation and deallocation mechanism managed by the garbage collector.

56
Q

What is the name of the software layer between a machine’s operating system and the Java bytecode, allowing Java programs to run on any system?

A

JVM

57
Q

If you had an existing, compiled Java program, which 3 packages contain the components that will execute the program?

A

JVM, JRE, JDK

58
Q

What does the following command line statement do?
» java Hello

A

Looks inside the compiled ‘Hello’ class for a “main” method and executes it.

59
Q

What does the following command line statement do?
» javac Hello.java

A

Compiles the Hello.java file.

60
Q

What is the Oracle JDK?

A

Java Development Kit. A superset of the JRE which contains everything that is in the JRE as well as tools such as the compilers and debuggers necessary for developing applications.

61
Q

What is the JRE?

A

Java Runtime Environment: provides the libraries, the Java Virtual Machine, and other components to run applications written in the Java programming language.

62
Q

What is OpenJDK?

A

It is the open source reference implementation of the Java development Kit.

63
Q

This component provides the libraries and other components required to run compiled applications written in the Java programming language.

A

JRE

64
Q

A virtual computer that is implemented in software allowing Java to run on many different operating systems.

A

JVM

65
Q

A superset of the Java libraries, which also contains everything required to run a Java application as well as tools such as the compilers and debuggers necessary for developing applications.

A

JDK

66
Q

Define the ‘main’ method

A

a special method in Java that serves as the entry point for the program execution.

67
Q

Which statement creates an instance of an array?

A

int[] numbers = new int[5];

also good

int numbers[] = new int[5];