JAVA Flashcards
What is the purpose of the main method in a Java program?
It is where the Java program starts, creating other objects and controlling the flow of the program.
How do you declare and initialize a String variable with the value “Hello World!”?
String str = “Hello World!”;
What symbol is used to concatenate two strings in Java?
The + symbol
How do you declare a constant in Java?
By using the final keyword.
final double STANDARD_GRAVITY = 9.81;
What is the result of the modulo operation 10 % 3?
1
What does the charAt method do in Java?
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
What is the difference between System.out.println and System.out.print?
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 do you extract a substring from index 1 to index 3 from the string “Birkbeck”?
String str1 = uni.substring(1, 3);
(This will store “ir”)
What is an example of declaring and assigning multiple variables in one line in Java?
int numberOfCars = 5, numberOfMotorbikes = 7;
What does the final keyword indicate when declaring a variable?
It indicates that the variable is a constant and its value cannot be changed.
How do you use the substring method to extract a part of a string in Java?
String sub = str.substring(startIndex, endIndex);
End index is excluded.
What data type would you use to store the value 9.81 in Java?
double
How do you declare a method in Java?
public void methodName() { /* code */ }
What does the + operator do when used with strings in Java?
It concatenates the strings.
What is the significance of using double quotes around a string in Java?
Double quotes are used to denote string literals in Java.
What is the effect of the statement x += 5; in Java?
It increments the variable x by 5.
What is the output of System.out.println(“Hello” + “ World!”);?
Hello World!
What is the role of the public keyword in Java?
It is an access modifier that makes the class, method, or variable accessible from any other class.
How do you declare an integer variable and assign it the value 10 in Java?
int x = 10;
What keyword is used to define a class in Java?
class
How do you create a new object of a class in Java?
ClassName obj = new ClassName();
What does the static keyword mean in Java?
It means the method or variable belongs to the class, rather than instances of the class.
How do you declare a method that returns an integer in Java?
public int methodName() { /* code */ }
What is the purpose of the void keyword in a method declaration?
It indicates that the method does not return any value.
How do you write a single-line comment in Java?
Using // followed by the comment text.
What is the default value of an uninitialized boolean variable in Java?
FALSE
How do you write a multi-line comment in Java?
Using /* comment text */
How do you import a package in Java?
Using the import statement, e.g., import java.util.Scanner;
How do you create a for loop that prints numbers 1 to 10 in Java?
for (int i = 1; i <= 10; i++)
{ System.out.println(i); }
How do you declare an array of integers with 5 elements in Java?
int[] arr = new int[5];
How do you access the third element of an array named arr in Java?
arr[2]
What does the length property of an array return in Java?
The number of elements in the array.
How do you handle exceptions in Java?
Using a try-catch block,
e.g.,
try { /* code / } catch (ExceptionType e) { / handler */ }
What is the purpose of the final block in exception handling?
It contains code that will run regardless of whether an exception was thrown or not.
How do you read a line of text from the Java console?
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
How do you create a new file using Java’s File class?
java File file = new File(“filename.txt”); file.createNewFile();
What does the == operator do with reference types in Java?
It checks if both references point to the same object.
How do you convert a string to an integer in Java?
int num = Integer.parseInt(“123”);
How do you compare the values of two strings in Java?
Using the .equals method, e.g., str1.equals(str2);
How do you convert an integer to a string in Java?
String str = String.valueOf(123);
String str = Integer.toString(123);
How do you create a Scanner object in Java to read user input from the keyboard?
Scanner scanner = new Scanner(System.in);
How do you import the Scanner class in Java?
import java.util.Scanner;
How do you read an integer value from the user using Scanner in Java?
int favouriteInteger = scanner.nextInt();
How do you read a double value from the user using Scanner in Java?
double favouriteDouble = scanner.nextDouble();
How do you read a string from the user using Scanner in Java?
String favouriteString = scanner.nextLine();
How do you solve the issue of reading a string after an integer input with Scanner?
Use an extra scanner.nextLine(); to consume the end-of-line character.
What is a common issue when using scanner.nextLine() after scanner.nextInt()?
The scanner does not consume the end-of-line character, so scanner.nextLine() may read it as input.
What is the syntax for an if-else if-else statement in Java?
if (condition1) { // statements }
else if (condition2) { // statements }
else { // statements }
How do you write a basic if statement in Java?
if (condition) { // statements }
How do you write a basic if-else statement in Java?
if (condition) { // statements } else { // statements }
What does the && operator mean in Java?
AND
What is the correct way to compare two strings for equality in Java?
str1.equals(str2);