APCSA Flashcards
(113 cards)
What is the default value of an int variable in Java?
0
What is the result of 7 / 2 in Java?
3
What is the result of 7.0 / 2 in Java?
3.5
What does the % (modulo) operator return?
The remainder after division
How do you declare a double variable called price?
double price;
What is the result of casting (int) 4.9?
4
Which Java class is used to generate random numbers?
java.util.Random
What is the purpose of Math.random()?
Generates a double between 0.0 (inclusive) and 1.0 (exclusive)
How do you import the Scanner class?
import java.util.Scanner;
How do you read an int from the console using Scanner?
Scanner sc = new Scanner(System.in); int x = sc.nextInt();
What method checks string equality?
.equals()
What method returns string length?
.length()
What method returns a character at index i of a String?
.charAt(i)
How do you concatenate two Strings?
Using the + operator
What is the output of ‘abc’.substring(1,3)?
bc
What is a class in Java?
A blueprint for creating objects
What is an object in Java?
An instance of a class
What keyword is used to create a new object?
new
What does the constructor do?
Initializes a new object
What is an instance variable?
A variable defined inside a class but outside methods
What is the visibility of public?
Accessible from any class
What is the visibility of private?
Accessible only inside the same class
What is a method?
A function defined inside a class
What is the purpose of a return type?
Specifies the data type the method returns