JAVA Flashcards
(130 cards)
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.