Name: Diya Budhathoki
Class: LGCG4
Tutorial 1
Task 1
What are the basic data type in java used to store whole numbers and decimal numbers?
Ans:
Decimal number- Float
Whole number- int
Task 2
Explain the difference between int and double data types in Java.
Ans:
Int: Represents integer without fractional component.
Size: 32 bytes (4 bits)
Exact values
Double: Represents floating numbers
Size: 64 bytes (8 bits)
Not always exact values
Task 3
What is the difference between the float and double data types in terms of precision and storage
space?
Ans:
The differences:
Float-Precision-
Single-precision floating-point type.
Typically stores 6–7 decimal digits of precision.
Less accurate for representing real numbers with many significant digits
Double- Precision
Double-precision floating-point type.
Typically stores 15–16 decimal digits of precision.
More accurate and suitable for applications requiring high precision.
Float-Storage space-
Occupies 4 bytes i.e. 32 bits of memory.
Double-Storage space
Occupies 8 bytes i.e. 64 bits of memory.
Task 4
How do you declare an integer variable in Java, and what is the range of values it can hold?
Ans:
An integer variable in Java can be declared using the int keyword.
The int data type in Java is a 32-bit signed integer, so it can hold values in the range:
−231-2^{31}−231 to 231−12^{31} - 1231−1, or -2,147,483,648 to 2,147,483,647.
Task 5: What is the significance of variable naming conventions in Java, and what are some best
practices?
Ans:
The significance of variable naming conventions in java are:
Readability
Clarity
Consistency
Avoid Errors
Some best practices are:
Lower Camel Case
Upper Camel Case
Screaming Snake case
Lower dot case
Kebab case
Task 6: How do you determine the data type of a variable in Java?
Check the declaration
Use reflection
IDE Assistance
Task 7
How can you perform addition, subtraction, multiplication, and division in Java using arithmetic
operators?
Task 8
What is the result of the expression 7 / 2 in Java, and how can you get the correct result if you want
to include the remainder?
Task 9
Explain the purpose of the modulus operator (%) in Java and provide an example.
The purpose of the modulus operator in java are:
1. Determining even or odd numbers.
2. Finding cyclic patterns (e.g., repeating sequences).
3. Performing operations on digits (e.g., extracting the last digit of a number).
4. Time calculations (e.g., converting seconds into minutes and seconds).
Checking odd or even
Task 10
Write a Java program to convert celsius to fahrenheit.
Task 11
Write a Java program to calculate the sum of two numbers the user enters and display the result.
Task 12
Can a constructor have a return type? Why or why not?
Ans:
No, a constructor cannot have a return type in Java.
In Java, a constructor does not have a return type because it is specifically designed to initialize an
object. It focuses solely on setting up the object's state. If a return type is specified, it behaves like a
regular method.
Task 13
What is the return type of the toString() method?
Ans:
The return type of the toString() method in Java is String.
Task 14
How do you override the toString() method and why is it considered a good practice to override it?
Overriding the toString() method in Java is important because it provides a clear, human-readable
representation of an object's state, which aids in debugging and understanding the code. The default
toString() method returns a class name and hash code, which is not helpful. Customizing the output
to show relevant attributes makes it easier for developers to inspect objects, especially when using
collections like ArrayList or HashMap. Overall, this practice enhances code maintainability and
readability.