Java Assignment- 1

Programming Basics

Basic Questions

  1. Write a Java program that declares int age = 22, double price = 199.99, and char grade = ‘A’, and then prints them on separate lines.
  2. Write a program that declares int a = 10 and int b = 3, and prints the sum, difference, product, integer quotient, and remainder.
  3. Write code that declares double x = 9.7, casts it to int y, and prints both x and y on separate lines.
  4. Write a program that uses relational operators to compare int p = 8 and int q = 12, and prints results of p == q, p != q, p < q, and p > q.
  5. Write a program that uses logical operators on booleans hasId = true and hasTicket = false, and prints hasId && hasTicket, hasId || hasTicket, and !hasTicket.
  6. Write a program that calculates and prints the value of the expression 10 + 3 * 4 – 6 / 2.
  7. Write a program that applies bitwise operators on int m = 6 and int n = 3: print m & n, m | n, m ^ n, and ~m.
  8. Write a program that prints the results of left shift and right shift for int r = 16 (print r << 1, r >> 1).
  9. Write a program that checks if int num = 14 is even or odd using if-else, and prints a clear message.
  10. Write a program that checks if int temp = -5 is positive, negative, or zero with if-else-if, and prints the result.
  11. Write a program that uses switch on int day = 2 to print “Monday” for 1, “Tuesday” for 2, and “Wednesday” for 3; print “Invalid day” otherwise.
  12. Write a for loop that prints numbers from 1 to 5 on one line separated by spaces.
  13. Write a while loop that prints numbers from 5 down to 1 on one line separated by spaces.
  14. Write a do-while loop that prints the message “Hello” exactly 3 times.
  15. Create an int[] arr = {2, 4, 6, 8} and use an enhanced for loop to print all elements and then print their sum.
  16. Write a method int addTwo() that returns 2 + 3. In main, call the method and print the returned value.
  17. Write a method int add(int a, int b) that returns the sum. In main, call it with 5 and 7 and print the result.
  18. Overload a method add: one version takes two int, another takes two double. In main, call both and print results.
  19. Write code to show pass-by-value with a primitive: define void setTo100(int x) that sets x = 100 inside the method. In main, declare int v = 5, call the method with v, and print v before and after the call.
  20. Write code to show changing an object’s field inside a method: define a class Box { int w; } and method void setWidth(Box b) that sets b.w = 100. In main, create a Box with w = 10, call the method, and print w before and after.

Intermediate Questions

  1. Write code that computes the average of three int marks 80, 75, 90 as a double. Use casting to avoid integer division and print the average.
  2. Write a method double divide(int a, int b) that returns a precise division result (not truncated). In main, call it with 7 and 2 and print the value.
  3. Write a program that checks whether int x = 16 is between 10 and 20 (inclusive) and is even, using relational and logical operators. Print a clear message.
  4. Write a program that finds the largest of int a = 10, b = 25, c = 15 using if-else-if, and prints the largest value.
  5. Write a program that uses a switch on String grade = “B” to print: A → “Excellent”, B → “Good”, C → “Average”, default → “Invalid grade”.
  6. Write a for loop that prints the multiplication table of 7 from 7 × 1 to 7 × 10 (one result per line).
  7. Write a while loop that reverses the digits of int n = 12345 and prints the reversed number 54321.
  8. Use a do-while loop to sum all elements of int[] a = {3, 5, 7, 9} and print the sum.
  9. Use an enhanced for loop to count how many even numbers are in int[] a = {1, 2, 3, 4, 5, 6} and print the count.
  10. Write a method boolean isPrime(int n) that returns whether n is prime. In main, call it for 17 and 18 and print the results.
  11. Write overloaded methods area: double area(double r) for circle (πr²) and int area(int w, int h) for rectangle (w*h). In main, call both and print results.
  12. Write code that swaps two integers using XOR (without a temp variable). Start with a = 5 and b = 9, then print values before and after the swap.
  13. Write a method boolean isOdd(int n) that returns true if n is odd using a bitwise operator (not %). Test it with n = 11 and print the result.
  14. Write a program that checks if int year = 2024 is a leap year using the standard rules and prints a clear message.
  15. Write a for loop from 1 to 15 that prints all numbers except those divisible by 3 (use continue).
  16. Write a for loop that adds numbers from 1 upward and stops when the running sum becomes greater than 20 (use break). Print the final sum.
  17. Write a method int increment(int x) that returns x + 1. In main, set int n = 5, print n, then call increment(n) and print the returned value and also print n again to show pass-by-value.
  18. Write a class Counter { int value; } and a method void inc(Counter c) that increases c.value by 1. In main, show value before and after the call.
  19. Write a method int max(int[] a) that returns the largest element. In main, test it on {2, 9, 5, 7} and print the result.
  20. Write code that assigns long big = 3000000000L; stores it in a float f, prints big and f, then casts f back to long and prints the result.

Advanced Questions

  1. Write a method boolean isPowerOfTwo(int n) that uses bitwise logic to return true only if n is a power of two (and greater than 0). Test it with 1, 2, 3, 4, 16.
  2. Write a method int calculate(int a, int b, char op) that uses switch to handle +, -, *, /, and %. In main, call it with a = 12, b = 5 for each operator and print each result.
  3. Write a method int countGreaterThan(int[] nums, int target) that returns how many elements are strictly greater than target. Test it with {3, 7, 2, 9, 4} and target = 4.
  4. Write a method that reverses an int[] array in place using two indexes (start and end). Test with {1, 2, 3, 4, 5} and print the array before and after.
  5. Write a method long factorial(int n) that computes the factorial using a for loop and returns a long. Test with n = 10 and print the result.
  6. Overload a method printArray to print arrays of int[], double[], and String[] using enhanced for. In main, test all three versions.
  7. Write code to show that reassigning a reference inside a method does not affect the caller’s reference: define void reassign(Box b) that creates b = new Box(); b.w = 999;. In main, create a Box with w = 5, call reassign, and print w before and after.
  8. Use nested for loops to print a right triangle of stars with 5 rows:
*
**
***
****
*****
  1. Write a method that merges two sorted int[] arrays into a new sorted int[] without using library sorting. Test with {1, 3, 5} and {2, 4, 6} and print the merged array.
  2. Write code that converts int n = 29 to a binary string using bitwise operations and a loop (do not use Integer.toBinaryString). Print the binary result.