Day 2
Variables and Data Tyes
Variables
• A variable is a container that holds data values.
• Variables are used to store and represent data in a program.
Examples:
int x = 100;
int age = 30;
float price = 150.50f;
double cost = 150.50;
String name = "John";
char grade = 'A';
Data Types
Data types define the type and size of data that can be stored in a variable.
1) Primitive Data Types
Primitive data types are the most basic data types in Java. They are predefined by the language and
named by a reserved keyword.
• byte
• short
• int
• long
• float
• double
• char
• boolean
2) Non-Primitive (Reference) Data Types
Non-primitive data types are created by the programmer and are not defined by Java (except for
String).
• String
• ArrayList
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
• HashMap
• HashSet
• Object
Size and Description of Primitive Data Types
Data Type Size Description
byte 1 byte Stores whole numbers from -128 to 127.
short 2 bytes Stores whole numbers from -32,768 to 32,767.
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647.
Stores whole numbers from -9,223,372,036,854,775,808 to
long 8 bytes
9,223,372,036,854,775,807.
float 4 bytes Stores fractional numbers, sufficient for storing 7 decimal digits.
double 8 bytes Stores fractional numbers, sufficient for storing 15 decimal digits.
boolean 1 bit Stores true or false values.
char 2 bytes Stores a single character/letter enclosed in single quotes (e.g., 'A').
Examples of Valid and Invalid Declarations
• char ch = 'ABC'; // Invalid: char can only store a single character.
• String ch = 'ABC'; // Invalid: String should use double quotes.
• String ch = 'A'; // Invalid: String should use double quotes.
• String ch = "A"; // Valid
• boolean b = true; // Valid
• boolean b = "true"; // Invalid: boolean should not use quotes.
• String s = true; // Invalid: String cannot store a boolean value.
• String s = "true"; // Valid
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Java Variables and Data Types Quiz
Question 1:
Which of the following is a correct way to declare a variable in Java? a) int 1stNumber = 10;
b) float number1 = 5.0f;
c) char letter = "A";
d) double value = '5.0';
Answer:
b) float number1 = 5.0f;
Question 2:
What is the size of an int in Java?
a) 4 bits
b) 8 bits
c) 16 bits
d) 32 bits
Answer:
d) 32 bits
Question 3:
Which data type would you use to store a single character?
a) String
b) char
c) boolean
d) int
Answer:
b) char
Question 4:
What is the default value of a boolean variable in Java?
a) true
b) false
c) 0
d) 1
Answer:
b) false
Question 5:
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
What is the result of the following code snippet?
a) 1020
b) 30
c) 20
d) 10
Answer:
b) 30
Question 6:
Which of the following is a valid float variable declaration?
a) float num = 3.14;
b) float num = 3.14d;
c) float num = 3.14f;
d) float num = 3.14F;
Answer:
c) float num = 3.14f;
d) float num = 3.14F;
Question 7:
What will be the output of the following code?
a) 2.5
b) 2
c) 2.0
d) 0
Answer:
c) 2.0
Question 8:
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Which keyword is used to declare a constant in Java?
a) final
b) static
c) const
d) fixed
Answer:
a) final
Question 9:
Which of the following is a correct way to declare a string in Java?
a) String name = 'John';
b) char name = "John";
c) String name = "John";
d) char name = 'J';
Answer:
c) String name = "John";
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan