JAVA Flashcards

1
Q

What is the purpose of the main method in a Java program?

A

It is where the Java program starts, creating other objects and controlling the flow of the program.

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

How do you declare and initialize a String variable with the value “Hello World!”?

A

String str = “Hello World!”;

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

What symbol is used to concatenate two strings in Java?

A

The + symbol

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

How do you declare a constant in Java?

A

By using the final keyword.

final double STANDARD_GRAVITY = 9.81;

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

What is the result of the modulo operation 10 % 3?

A

1

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

What does the charAt method do in Java?

A

It returns the character at a specified index in a string.

String message = “Hello, World!”;

char firstChar = message.charAt(0);
//Retrieves character at index 0

char seventhChar = message.charAt(6);
//Retrieves character index 6

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

What is the difference between System.out.println and System.out.print?

A

System.out.println prints a string or number and then starts a new line, while System.out.print prints a string or number without starting a new line.

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

How do you extract a substring from index 1 to index 3 from the string “Birkbeck”?

A

String str1 = uni.substring(1, 3);
(This will store “ir”)

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

What is an example of declaring and assigning multiple variables in one line in Java?

A

int numberOfCars = 5, numberOfMotorbikes = 7;

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

What does the final keyword indicate when declaring a variable?

A

It indicates that the variable is a constant and its value cannot be changed.

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

How do you use the substring method to extract a part of a string in Java?

A

String sub = str.substring(startIndex, endIndex);

End index is excluded.

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

What data type would you use to store the value 9.81 in Java?

A

double

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

How do you declare a method in Java?

A

public void methodName() { /* code */ }

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

What does the + operator do when used with strings in Java?

A

It concatenates the strings.

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

What is the significance of using double quotes around a string in Java?

A

Double quotes are used to denote string literals in Java.

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

What is the effect of the statement x += 5; in Java?

A

It increments the variable x by 5.

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

What is the output of System.out.println(“Hello” + “ World!”);?

A

Hello World!

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

What is the role of the public keyword in Java?

A

It is an access modifier that makes the class, method, or variable accessible from any other class.

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

How do you declare an integer variable and assign it the value 10 in Java?

A

int x = 10;

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

What keyword is used to define a class in Java?

A

class

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

How do you create a new object of a class in Java?

A

ClassName obj = new ClassName();

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

What does the static keyword mean in Java?

A

It means the method or variable belongs to the class, rather than instances of the class.

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

How do you declare a method that returns an integer in Java?

A

public int methodName() { /* code */ }

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

What is the purpose of the void keyword in a method declaration?

A

It indicates that the method does not return any value.

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

How do you write a single-line comment in Java?

A

Using // followed by the comment text.

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

What is the default value of an uninitialized boolean variable in Java?

A

FALSE

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

How do you write a multi-line comment in Java?

A

Using /* comment text */

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

How do you import a package in Java?

A

Using the import statement, e.g., import java.util.Scanner;

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

How do you create a for loop that prints numbers 1 to 10 in Java?

A

for (int i = 1; i <= 10; i++)
{ System.out.println(i); }

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

How do you declare an array of integers with 5 elements in Java?

A

int[] arr = new int[5];

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

How do you access the third element of an array named arr in Java?

A

arr[2]

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

What does the length property of an array return in Java?

A

The number of elements in the array.

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

How do you handle exceptions in Java?

A

Using a try-catch block,
e.g.,

try { /* code / } catch (ExceptionType e) { / handler */ }

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

What is the purpose of the final block in exception handling?

A

It contains code that will run regardless of whether an exception was thrown or not.

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

How do you read a line of text from the Java console?

A

Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();

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

How do you create a new file using Java’s File class?

A

java File file = new File(“filename.txt”); file.createNewFile();

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

What does the == operator do with reference types in Java?

A

It checks if both references point to the same object.

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

How do you convert a string to an integer in Java?

A

int num = Integer.parseInt(“123”);

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

How do you compare the values of two strings in Java?

A

Using the .equals method, e.g., str1.equals(str2);

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

How do you convert an integer to a string in Java?

A

String str = String.valueOf(123);

String str = Integer.toString(123);

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

How do you create a Scanner object in Java to read user input from the keyboard?

A

Scanner scanner = new Scanner(System.in);

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

How do you import the Scanner class in Java?

A

import java.util.Scanner;

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

How do you read an integer value from the user using Scanner in Java?

A

int favouriteInteger = scanner.nextInt();

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

How do you read a double value from the user using Scanner in Java?

A

double favouriteDouble = scanner.nextDouble();

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

How do you read a string from the user using Scanner in Java?

A

String favouriteString = scanner.nextLine();

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

How do you solve the issue of reading a string after an integer input with Scanner?

A

Use an extra scanner.nextLine(); to consume the end-of-line character.

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

What is a common issue when using scanner.nextLine() after scanner.nextInt()?

A

The scanner does not consume the end-of-line character, so scanner.nextLine() may read it as input.

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

What is the syntax for an if-else if-else statement in Java?

A

if (condition1) { // statements }
else if (condition2) { // statements }
else { // statements }

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

How do you write a basic if statement in Java?

A

if (condition) { // statements }

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

How do you write a basic if-else statement in Java?

A

if (condition) { // statements } else { // statements }

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

What does the && operator mean in Java?

A

AND

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

What is the correct way to compare two strings for equality in Java?

A

str1.equals(str2);

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

What is the syntax for a while loop in Java?

A

while (condition) { // statements }

46
Q

What is the syntax for a do-while loop in Java?

A

do { // statements } while (condition);

47
Q

How do you write a basic for loop in Java?

A

for (int i = 0; i < 10; i++) { // statements }

48
Q

What is the scope of a variable declared inside a loop in Java?

A

The variable is only defined within the loop.

49
Q

How do you exit a loop prematurely in Java?

A

Use the break statement.

49
Q

What is the purpose of continuing the statement in a loop?

A

It skips the current iteration and proceeds with the next iteration of the loop.

50
Q

How do you create a nested loop in Java?

A

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// statements
}
}

51
Q

What is a common use of nested loops?

A

Generating a multiplication table.

52
Q

How do you declare a boolean variable in Java?

A

boolean isJavaFun = true;

53
Q

What is the syntax for an if statement that checks multiple conditions using &&?

A

if (condition1 && condition2) { // statements }

54
Q

How do you check if a number is positive in Java?

A

if (number > 0) { // statements }

55
Q

How do you declare and initialize an array of integers with 5 elements?

A

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

56
Q

How do you access the length of an array in Java?

A

array.length

57
Q

How do you generate a random number between 0 (inclusive) and 1 (exclusive) in Java?

A

double r = Math.random();

58
Q

What is the syntax for checking if a variable x is between 0 and 100 (inclusive)?

A

if (0 <= x && x <= 100) { // statements }

58
Q

How do you convert a string to a double in Java?

A

double num = Double.parseDouble(“123.45”);

58
Q

How do you print an array of integers in Java?

A

for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }

59
Q

What is the syntax for the ternary operator in Java and how is it used?

A

Syntax:
condition ? expressionIfTrue : expressionIfFalse;

String result = (age >= 18) ? “You can drive” : “You cannot drive”;

59
Q

How do you declare an array of integers in Java?

A

int[] scoreBoard;

60
Q

How do you construct a new array of integers with 5 elements in Java?

A

new int[5];

61
Q

How do you declare and initialize an array with specific values in Java?

A

int[] scoreBoard = {10, 30, 42, 11, 45};

62
Q

How do you initialize an array of 5 integers to zero in Java?

A

int[] scoreBoard = new int[5];

63
Q

How do you declare and initialize a string array with specific values in Java?

A

String[] classmates = {“Maria”, “John”, “Irene”};

63
Q

How do you access the 4th element of an array named scoreBoard in Java?

A

scoreBoard[3];

63
Q

What happens if you try to access an index outside the bounds of an array in Java?

A

It causes a runtime exception: java.lang.ArrayIndexOutOfBoundsException.

64
Q

How do you get the length of an array named scoreBoard in Java?

A

scoreBoard.length;

65
Q

How do you iterate through all elements of an array using an enhanced for loop in Java?

A

for (int playerScore : scoreBoard) { System.out.print(playerScore + “ “); }

66
Q

How do you fill an array with values provided by the user in Java?

A

for (int i = 0; i < scoreBoard.length; i++) { scoreBoard[i] = scanner.nextInt(); }

66
Q

How do you copy the elements of one array to another in Java?

A

for (int i = 0; i < scoreBoard.length; i++) { pointBoard[i] = scoreBoard[i]; }

67
Q

How do you parse a command-line argument to an integer in Java?

A

int num = Integer.parseInt(args[0]);

67
Q

How do you insert an element at an arbitrary position in an array in Java?

A

Shift elements to the right and then insert the element.

68
Q

How do you declare a partially filled array and keep track of the filled positions?

A

int[] array = new int[10]; int rightmost = 5;

69
Q

How do you swap two elements in an array in Java?

A

int temp = scoreBoard[0]; scoreBoard[0] = scoreBoard[1]; scoreBoard[1] = temp;

70
Q

What is the basic idea of the selection sort algorithm?

A

It divides the array into sorted and unsorted parts and iteratively selects the smallest element from the unsorted part to move to the sorted part.

70
Q

How do you declare an ArrayList of strings in Java?

A

ArrayList<String> items = new ArrayList<String>();</String></String>

70
Q

How do you add an element to an ArrayList in Java?

A

items.add(“Sword”);

71
Q

How do you get the size of an ArrayList in Java?

A

items.size();

72
Q

How do you check if an ArrayList is empty in Java?

A

items.isEmpty();

73
Q

How do you remove an element at a specific position in an ArrayList in Java?

A

items.remove(index);

74
Q

How do you set the value of an element at a specific position in an ArrayList in Java?

A

items.set(index, “Helm”);

75
Q

How do you create a copy of an ArrayList in Java?

A

ArrayList<String> copy = new ArrayList<String>(items);</String></String>

75
Q

How do you iterate through all elements of an ArrayList using an enhanced for loop?

A

for (String item : items)
{
System.out.println(item);
}

76
Q

How do you declare a two-dimensional array in Java?

A

int[][] example = { { 1, 0, 1 }, { 1, 1, 0 }, { 0, 0, 1 } };

77
Q

How do you access an element in a two-dimensional array in Java?

A

example[row][column];

78
Q

How do you populate a two-dimensional array using nested loops?

A

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
array[i][j] = (i + 1) * (j + 1);
}
}

78
Q

How do you convert a string to a double in Java?

A

double num = Double.parseDouble(“123.45”);

78
Q

How do you generate a random integer between 0 and 100 in Java?

A

int randomNum = (int)(Math.random() * 101);

79
Q

How do you print all elements of an array in Java?

A

for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}

80
Q

What is the main purpose of object-oriented programming (OOP)?

A

To organize software design around data, or objects, rather than functions and logic.

81
Q

What is a class in object-oriented programming?

A

A blueprint for creating objects, providing initial values for state (attributes) and implementations of behaviour (methods). Example: class Animal { int age; String name; }

81
Q

What does a constructor in Java do?

A

Initializes a new object and its properties when an instance is created. Example: public Animal() { age = 0; name = “”; }

82
Q

What is an example of a simple Java constructor for a class Employee?

A

public Employee() { this.name = “”; this.email = “”; this.workedHours = 0; }

83
Q

What are access modifiers in Java?

A

Keywords that set the accessibility of classes, methods, and other members. Common modifiers include public, private, and protected. Example: private int age;

83
Q

What does the private access modifier do?

A

Restricts access to the member so that it can only be accessed within its own class. Example: private String password;

83
Q

How can private variables in a class be accessed from outside the class?

A

Through public methods, typically getters and setters. Example: public String getName() { return name; }

84
Q

How do you declare a class in Java?

A

Using the class keyword followed by the class name and a pair of curly braces. Example: class Employee { }

85
Q

How is a new object instantiated in Java?

A

The new keyword is followed by the class name and parentheses. Example: Employee emp = new Employee();

85
Q

What is the difference between public and private variables?

A

Public variables can be accessed from any other class; private variables can only be accessed within their own class. Example: public int id; private String name;

86
Q

What is the default value of a string in Java if not explicitly initialized?

A

null
Example: String name;

87
Q

How does a for loop differ from a while loop?

A

A for loop is used when the number of iterations is known before it runs; a while loop runs based on a condition being true. Example: for (int i = 0; i < 10; i++) {} vs while (condition) {}

87
Q

Why are constructors important in Java?

A

They provide a way to initialize the state of new objects at the moment of creation. Example: public Employee(String name) { this.name = name; }

88
Q

How do you create a method in Java that returns an integer?

A

public int getAge() { return age; }

88
Q

What is method overloading?

A

When two or more methods in the same class have the same name but different parameters. Example: void display(String data) {} void display(int data) {}

89
Q

What is a getter method in Java?

A

A method that reads and returns the value of a private variable. Example: public int getAge() { return this.age; }

89
Q

Why should instance variables often be private?

A

To maintain encapsulation and prevent unauthorized parts of code from modifying internal state. Example: private int age;

90
Q

What is a setter method in Java?

A

A method that updates the value of a private variable. Example: public void setAge(int age) { this.age = age; }

91
Q

How do you handle data encapsulation in Java?

A

By keeping class fields private and providing public getters and setters to access and update the values. Example: public void setName(String name) { this.name = name; }

91
Q

What does the this keyword represent in Java?

A

It refers to the current instance of the class. Example: this.name = name;

92
Q

How do you add an element to an ArrayList in Java?

A

Using the .add() method. Example: list.add(“element”);

93
Q

What is the purpose of the public keyword in a class definition?

A

To allow access to classes, methods, or variables from any other class. Example: public void display() {}

93
Q

What is an instance variable?

A

A variable declared within a class for which each instantiated object of the class has its own copy. Example: private int id;

94
Q

How do you check the size of an ArrayList?

A

Using the .size() method. Example: list.size();

94
Q

What is method visibility and why is it important?

A

It dictates from where the method can be accessed within the application, crucial for encapsulation. Example: private void calculateSalary() {}

94
Q

How does the default constructor behave if not explicitly defined?

A

It initializes object fields with default values, such as null for objects and zero for numeric types. Example: When no constructor is defined, Employee e = new Employee();

95
Q

How do you declare a method that does not return a value?

A

With the void keyword. Example: public void printName() { System.out.println(this.name); }

96
Q

What is the role of a constructor in an object’s lifecycle?

A

To initialize the object’s state when it is created. Example: public Person() { this.name = “Unknown”; this.age = 0; }

97
Q

How are instance methods called in Java?

A

Using the dot notation with an instance of the class. Example: person.displayInfo();

98
Q

What is the significance of the new keyword in Java?

A

It creates a new object instance of a class. Example: List<String> myList = new ArrayList<>();</String>