Basic Java Assignments

Below are few Java assignments you can try to develop programs, and get more confidence in Java.
1a. Declare one dimensional array of primitive integers, of size 20.
Write a program to reverse the elements of array, and print reversed array.

1b. From a given integer, extract each digit, and display each digit. NOTE: integer can have any number of digits(ofcourse within integer range)

1c. Display all numbers between 1 and 40, except number, which is multiple of both 2 and 3.


2.Declare two dimensional arrays of primitive floats, with 6 rows and 8 columns.
Write a program to exchange row and column numbers, and print the final elements in rows and columns.

3.Write a class, with a method public void show(), and print Hello World message from this method.
In main method create object of above class, and invoke show() method.

4.In addition to above Assignment 3, add a default constructor[print message in constructor body] to the class.
In main method create object of above class, and invoke show() method. In output observe the order in which the messages are printed.

5.Write a class(say class A) with few data members, and methods.In another class write below methods which
a.takes object of class A as parameter,
b.create object of class A and return the object.

6.Method overload: write a class with 4 methods named add() with different number and type of parameters.add() methods need to return added value of parameters.
From main method invoke the 4 add() methods, get the return values, print them.

7.Write a class with four static methods(add, , each accepting 2 parameters )computes mathematical operation and prints the result.Invoke these static methods from main method.

8.Write a program with all usages of this keyword.
For eg. this(), this. , this

9.Invoke static method, and use static data members from another static method.
Also invoke non static method, and use non static data members from a static method.
observe the difference…

10.Serialization:Write an Employee class with id,name, father name, photo url as data members. use transient keyword for photo url. Serialize the object of Employee class and de serialize it. print data members of de serialized object.

11.Open file using FileInputStream, read one line at a time from file, and print lines on console.
Hint:use BufferedReader

12.Use SequenceInputStream, to combine input from four different files, and write all the content to fifth file.

13.Create a class with 2 data members of any type, along with get and set methods. Then declare an array of this class type, and store objects in the array. Now iterate(using for loop) through each object in array, print object details with get method.

14.in main method throw an exception, using throw keyword, catch the exception using catch block. In catch block, further throw the exception.Hint: use throws keyword also

15.Which is base class of all classes in Java?

16.Write a class which inherits from String class, observe it.Also try to inherit from StringBuffer, Integer, Float, classes also.

17.Write a program for Nested try blocks, and observe the execution flow.

18.In Collection, what is difference between Object based Collection and Generic version Collection? What are the scenarios in which each of them are preferred.

19.What is output of below program

public class Test{
    void sum(int i,int j)
    {
        int k=0;
        System.out.println("Hello");
        sum(i,j);
    }

    public static void main(String args[])
    {
        Test obj = new Test();
        obj.sum(10,20);
    }
}
Ans: Exception in thread "main" java.lang.StackOverflowError occurs
20.Using printStackTrace(), get the stack trace into a String, instead of printing to console
21.There is a file with some data as shown below
f1,f2
f4,f6
f2,f1
f2,f3
f3,f4

f1,f2 means f1 is friend of f2, and so on. Write a program to read contents of this file and remove duplicate entries, and write non duplicated entries to another file.

22. Is it possible to change the current object to which this is reference? In other words, is below statement  valid in Java
this = new Xyz();//Xyz is some class

23. A two dimensional Array with 4 rows and 5 columns, and type float. Add elements of each row, display total and average of each row.


Servlets interview Questions

  1. Using Servlets develop page view counter, count value can be maintained in database. Use cookies to find unique visits to the web page
  2. JSP write Filter, with a specific url pattern. And check that Filter need to get invoked only when request url matches the pattern
  3. Write a Filter, for JSP, which checks the ip address from where request has originated, return without processing request if the request ip address is in a specific range
  4. On webpage provide option to select background color of the page, store this back ground color in cookie. Later, when user displays the cookie value(background color), to display the webpage with selected back ground color
  5. Create custom JSP tag, use this custom tag in your JSP code


You may also like to read:
Core Java Assignments 1