Exercise Overview

How to Use These Exercises

These exercises are designed to reinforce your understanding of Java data types, variables, and type conversion. Start with the easy exercises and progress to more challenging ones as you build confidence.

Tips for Success:

  • Understand the difference between primitive and reference types
  • Practice safe type conversion and casting
  • Use appropriate data types for your variables
  • Follow Java naming conventions
  • Test your programs with various data types
3 Easy Exercises
3 Medium Exercises
2 Hard Exercises

Easy Exercises - Start Here!

2.1

Variable Declaration and Initialization

Easy

Create variables of different data types and initialize them with appropriate values.

Requirements:

  • Declare an integer variable for age
  • Declare a double variable for height
  • Declare a boolean variable for isStudent
  • Declare a String variable for name
  • Declare a char variable for grade
  • Initialize all variables with sample values

Expected Output:

Age: 25
Height: 1.75
Is Student: true
Name: John Doe
Grade: A

Hints:

  • Use appropriate data types for each variable
  • Remember that char values use single quotes
  • String values use double quotes
  • Display each variable with a descriptive label

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_1.zip. Maximum file size: 5MB.
2.2

Basic Type Conversion

Easy

Practice converting between different data types using casting and conversion methods.

Requirements:

  • Convert an int to double (widening conversion)
  • Convert a double to int (narrowing conversion)
  • Convert a String to int using Integer.parseInt()
  • Convert an int to String using String.valueOf()
  • Display the results of each conversion

Sample Output:

Original int: 42
Converted to double: 42.0
Original double: 3.99
Converted to int: 3
String "123" converted to int: 123
Int 456 converted to String: 456

Hints:

  • Use explicit casting with parentheses: (double) intValue
  • Remember that narrowing conversions truncate decimal parts
  • Use Integer.parseInt() for String to int conversion
  • Use String.valueOf() for int to String conversion

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_2.zip. Maximum file size: 5MB.
2.3

Constants and Final Variables

Easy

Create constants and final variables to understand immutability in Java.

Requirements:

  • Create a final constant for PI (3.14159)
  • Create a final constant for MAX_AGE (100)
  • Create a final String for COMPANY_NAME
  • Create a final boolean for DEBUG_MODE
  • Display all constants
  • Attempt to modify these constants (should cause compilation error)

Expected Output:

Constants:
PI: 3.14159
MAX_AGE: 100
COMPANY_NAME: TechCorp
DEBUG_MODE: true

Hints:

  • Use the final keyword to create constants
  • Follow naming convention: UPPER_SNAKE_CASE for constants
  • Constants must be initialized when declared
  • Try to reassign a constant to see the compilation error

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_3.zip. Maximum file size: 5MB.

Medium Exercises - Build Your Skills!

2.4

Variable Scope and Lifetime

Medium

Understand how variable scope affects accessibility and lifetime.

Requirements:

  • Create a class with instance variables
  • Create local variables in methods
  • Demonstrate variable shadowing
  • Show the difference between instance and local variables
  • Create a method that uses both types of variables

Sample Output:

Instance variable: 10
Local variable: 20
Shadowed variable: 30
After method call:
Instance variable: 10
Local variable: 20

Hints:

  • Instance variables are declared outside methods
  • Local variables are declared inside methods
  • Variable shadowing occurs when local variable has same name as instance variable
  • Use this keyword to access instance variable when shadowed

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_4.zip. Maximum file size: 5MB.
2.5

String Operations and Manipulation

Medium

Practice working with String variables and common string operations.

Requirements:

  • Concatenate strings using + operator
  • Use String.format() for formatted strings
  • Find the length of a string
  • Convert string to uppercase and lowercase
  • Check if a string contains a substring
  • Extract a substring from a string
  • Replace characters in a string

Sample Output:

Original: Hello World
Length: 11
Uppercase: HELLO WORLD
Lowercase: hello world
Contains "World": true
Substring (0,5): Hello
Replaced: Hello Java

Hints:

  • Use + operator for concatenation
  • Use String.format() with format specifiers like %s
  • Use length() method to get string length
  • Use toUpperCase() and toLowerCase() methods
  • Use contains() method for substring checking
  • Use substring() method with start and end indices
  • Use replace() method to replace characters

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_5.zip. Maximum file size: 5MB.
2.6

Wrapper Classes and Autoboxing

Medium

Work with wrapper classes and understand autoboxing/unboxing.

Requirements:

  • Create wrapper class objects (Integer, Double, Boolean, Character)
  • Demonstrate autoboxing (primitive to wrapper)
  • Demonstrate unboxing (wrapper to primitive)
  • Use wrapper class methods (parseInt, toString, etc.)
  • Handle null values with wrapper classes
  • Compare primitive and wrapper class behavior

Sample Output:

Primitive int: 42
Wrapper Integer: 42
Autoboxing: 100
Unboxing: 100
Parsed from String: 123
Wrapper to String: 42
Null wrapper: null

Hints:

  • Use Integer.valueOf() to create Integer objects
  • Autoboxing happens automatically: Integer i = 42;
  • Unboxing happens automatically: int x = integerObject;
  • Use Integer.parseInt() to parse strings
  • Wrapper classes can be null, primitives cannot
  • Use toString() method to convert to string

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_6.zip. Maximum file size: 5MB.

Hard Exercises - Master the Concepts!

2.7

Advanced Type Conversion and Validation

Hard

Create a robust type conversion system with validation and error handling.

Requirements:

  • Create a method to safely convert String to int with validation
  • Create a method to safely convert String to double with validation
  • Handle NumberFormatException for invalid conversions
  • Create a method to check if a string represents a valid number
  • Implement range checking for converted values
  • Create a method to format numbers with specific precision
  • Test with various input scenarios (valid, invalid, edge cases)

Sample Output:

Converting "123": Success - 123
Converting "abc": Error - Invalid number format
Converting "3.14": Success - 3.14
Converting "999999999999": Error - Number too large
Valid number check: "42" -> true
Valid number check: "abc" -> false
Formatted number: 3.14159 -> 3.14

Hints:

  • Use try-catch blocks to handle NumberFormatException
  • Use Integer.parseInt() and Double.parseDouble()
  • Check for null or empty strings before conversion
  • Use String.matches() with regex for validation
  • Use String.format() for number formatting
  • Consider using wrapper classes for nullable results

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_7.zip. Maximum file size: 5MB.
2.8

Data Type Performance Analysis

Hard

Analyze and compare the performance characteristics of different data types.

Requirements:

  • Create a program to measure memory usage of different data types
  • Compare performance of primitive vs wrapper classes
  • Measure time for arithmetic operations on different numeric types
  • Analyze string concatenation performance
  • Create a memory-efficient data structure for a specific use case
  • Implement a custom data type with specific constraints
  • Generate a performance report with recommendations

Sample Output:

Memory Usage Analysis:
int: 4 bytes
Integer: 16 bytes
String "Hello": 24 bytes
double: 8 bytes
Double: 24 bytes

Performance Comparison:
Primitive addition: 0.001ms
Wrapper addition: 0.005ms
String concatenation: 0.010ms

Recommendations:
- Use primitives for performance-critical operations
- Use wrappers when null values are needed
- Prefer StringBuilder for multiple string operations

Hints:

  • Use System.currentTimeMillis() for timing
  • Use Runtime.getRuntime().totalMemory() for memory analysis
  • Create large arrays to measure memory differences
  • Use StringBuilder for efficient string operations
  • Consider object overhead when comparing primitives vs wrappers
  • Use loops to create measurable performance differences

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Please compress your Java file and any related files into a ZIP archive. Name your file as: lastname_first_name_course_year_section_exercise_8.zip. Maximum file size: 5MB.

🎉 Module 2 Exercises Complete!

You've practiced Java data types, variables, and type conversion. Ready to move on to control structures!