What is the correct way to declare an integer variable named count?
private int count;
How can you concatenate two strings firstName and lastName in Java?
String fullName = firstName + lastName;
What is an object in programming?
An object is an instance of a class that encapsulates data and behavior.
What is the syntax for an if statement in Java?
if (condition) { // code to execute }
What is the basic syntax to define a new class namedPerson?
public class Person { // class body }
How do you assign the value 5 to a double variable named temperature?
double temperature = 5.0;
What method would you use to find the length of a string?
stringVariable.length();
Define a constructor and its purpose.
A constructor is a special method that initializes an object when it is created.
How can you check if a boolean variableisReadyis false?
if (!isReady) { // code }
How do you define a method that takes an int parameter and returns a boolean?
public boolean methodName(int parameter) { // implementation }
What is the difference between integer and floating-point arithmetic?
Integer arithmetic deals with whole numbers, while floating-point arithmetic can represent decimal values.
Describe how to generate a random integer between 1 and 10 with the Math class?
int randomNum = (int)(Math.random() * 10) + 1;
How do you access a global variable from inside a class?
this.instanceVariableName
What are the three main Boolean operators in Java?
AND (&&), OR (||), NOT (!)
Describe the role of instance variables in a class.
Instance variables hold the state of an object and are specific to each instance of the class.
In Java, what is the keyword used to define a constant?
final
What is the output of the following code: System.out.println(Math.abs(-5));?
5
What is the difference between public and private instance variables?
Public variables can be accessed from outside the class, while private variables can only be accessed within the class.
What is the result of the following
int x = 5, y = 6;
int result = !(x<5 || y>5);
result = false
What is an API documentation and how is it useful?
API documentation provides detailed information about classes, methods, and functionalities available in a programming language, helping developers to understand and use them effectively.
Explain the significance of variable scope in programming.
Variable scope determines where a variable can be accessed and is typically defined by where it is declared (e.g., local vs. instance scope).
Explain how you would round a double to the nearest whole number.
Math.round(doubleValue);
Explain how you would define a class named
Car with a method startEngine()
public class Car {
public void startEngine() {
// implementation
}
}
What is the result of the expression true && (true || (false && true))?
true
Define how you would perform unit testing using a simplemain()method.
By creating an instance of the class and calling the methods to verify expected outcomes, e.g.,public static void main(String[] args) { /* testing code */ }