STUDY INDIVIDUAL ASSIGNMENT
STUDY INDIVIDUAL ASSIGNMENT
DIPLOMA IN INFORMATION
COMMUNICATION TECHNOLOGY
Question:
1. What is a variable?
2. Explain the rules and conventions for naming variables in Java.
3. List and briefly explain all the primitive data types in Java. Provide examples of each data type.
4. What is the difference between declaring a variable and initializing it? Provide an example to illustrate
the difference.
5. List all arithmetic operators available in Java and explain their functions with examples.
6. Describe the purpose of relational operators in Java. Give examples of how they are used. 7. Explain the
logical operators (&&, |, !) with examples of their use.
8. Explain how assignment operators work, providing examples.
9. What is type casting in Java?
10.Explain the difference between implicit and explicit type casting. Give examples of each type of
casting.
11.Explain how string concatenation works in Java. Provide an example of concatenating two strings.
12.What is the difference between int and double data types?
13.Why would you use a boolean data type in a program?What is the output of the following expression: 5
+ 3 * 2? Explain the result.
14.If a variable is declared as final int MAX = 100;, what does the final keyword indicate? Can the value
of MAX be changed later? Why or why not?
15.What happens if you try to divide an integer by zero in Java? Explain the result.
16.Why is it important to understand data types and operators when writing Java programs? 17.How does
incorrect use of data types and operators lead to errors in your code?
1.What is a variable?
A variable can be thought of as a labeled box in which you store data that your program
will use or change while it is running. Each such box has a name, a type and a value;
each variable does, too. Variables are a necessity because they enable us to provide a
way of organizing and tracking information in our programs.
Use camelCase where every word, except the first, starts with a capital letter; such as
totalScore or isAvailable. Use names that indicate what your program is doing.
3. Primitive Data Types in Java Java has eight "primitive" types that are designed to hold
specific kinds of data:
byte: Smallest integer; range is -128 to 127. Example: byte age = 25; short: Slightly bigger
integer; range is -32,768 to 32,767. Example: short distance =2000;
int: Standard whole number; range is approximately -2 billion to 2 billion.
Examples: int population = 1000000;
long: Very large whole number; usage when it is out of int range.
Examples: long distanceToMoon = 384400000L;
Example:
int age = 20;
boolean isAdult = age >= 18; // true, since 20 is greater than 18
7. Logical Operators (&&, ||, !)
1. && (AND): True if both conditions are true.
boolean canVote = (age >= 18) && (citizen == true);
3. ! (NOT): Flips the value; true becomes false, and vice versa.
boolean notWeekend = !(isWeekend); // true if isWeekend is false
• Explicit Casting: You manually convert larger types to smaller types, like from
double to int.
double price = 9.99;
int priceInt = (int) price; // Manually turns 9.99 into 9