Chapter 1 Exercises
Exercise 1. Declare three variables of different types: one variable of type int, another of type String, and a third of type char. Directly assign the value 25 to the int variable, “Hello world” to the String variable, and the character ‘A’ to the char variable.
Solution:
int amount = 25;
char character = 'A';
String message = "Hello world";
💡 Use descriptive names: Choose variable names that are clear and meaningful. A good variable name makes the code easier to understand.
Exercise 2. Declare three variables of different types: one variable of type int, another of type String, and a third of type char. Then, in different lines of code, assign the following values to each: the value 25 to the int variable, the value “Hello world” to the String variable, and the character ‘A’ to the char variable.
Solution:
int amount;
char character;
String message;
amount = 25;
character = 'A';
message = "Hello world";
Exercise 3. Declare and assign a value to a double variable that represents the number π (pi).
Solution:
double pi = 3.14159;
Exercise 4. In a mysterious world of adventures, you are exploring an ancient temple full of traps. You must create a Java program that declares a boolean variable named activated and assigns it the value true, indicating that you have activated the temple’s security mechanism.
Solution:
boolean activated = true;
💡 Initialize your variables: In your first programs, initialize your variables when you declare them. This prevents errors and unexpected behavior in your program.
Exercise 5. In the following lines of code, two variables are declared: num and text. Assign them the values 55 and “Hello”, respectively.
public class Main {
public static void main(String[] args) {
int num;
String text;
}
}
Solución:
num = 55;
text = "Hello";
Exercise 6. Tell me the difference between a variable and a data type.
Solution:
A variable is a memory space with an associated name where a specific value can be stored and manipulated, while a data type specifies what kind of value that variable can contain (such as integer, string, etc.). In summary, the difference is that the variable is the container, and the data type is the characteristic of the value stored in that container.
Exercise 7. Look at this code and identify the variables and their respective values.
public class Main {
public static void main(String[] args) {
int x = 5;
double y = 3.14;
boolean z = true;
System.out.println("The value of x is: " + x);
System.out.println("The value of y is: " + y);
System.out.println("The value of z is: " + z);
}
}
Solution:
In this exercise, the variables are x, y, and z, and their values are:
x has the value 5
y has the value 3.14
z has the value true
Exercise 8. Look at this code and identify the variables and their respective values.
public class Main {
public static void main(String[] args) {
String name = "John";
int age = 30;
System.out.println("The name is: " + name);
System.out.println("The age is: " + age);
}
}
Solution:
In this exercise, the variables are name and age, and their values are:
name has the value "John"
age has the value 30
Exercise 9. Look at this code and identify the variables and their respective values.
public class Main {
public static void main(String[] args) {
char letter = 'A';
boolean isUppercase = true;
System.out.println("The letter is: " + letter);
System.out.println("Is it uppercase?: " + isUppercase);
}
Solution:
In this exercise, the variables are letter and isUppercase, and their values are:
letter has the value 'A'
isUppercase has the value true
💡 Follow the naming convention: Use the camelCase convention to name variables in Java (for example, myVariable). This improves readability and aligns with common practices in Java.
Exercise 10. Look at this code and identify the variables and their respective values
public class Exercise10 {
public static void main(String[] args) {
double radius = 2.5;
int counter = 10;
System.out.println("The radius is: " + radius);
System.out.println("The counter is: " + counter);
}
}
Solución:
In this exercise, the variables are radius and counter, and their values are:
radius has the value 2.5
counter has the value 10