Pages

Programming Questions and Exercises : On Class Constructor

Question 1
Circle ClassCreate a class to represent a Circle type in java, which should have following :
radius. Instance field of type double
No-argument constructor. Set radius with default value of 1.0
Constructor. that accept an argument for radius
getRadius. public method that returns the radius of Circle
getArea. Method that returns the area of Circle

public class Circle
{
    private double radius;

    public Circle()
    {
        radius = 1.0;
    }

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public void setRadius(double radius)
    {
        this.radius = radius;
    }

    public double getRadius()
    {
        return radius;
    }
}

Question 2
Pet ClassCreate a class to represent a Pet type in java, which should have the following fields:
name. Instance variable of type String that holds the name of a pet.
animal. Instance variable of type String that holds the type of animal that a pet is.
age. Instance variable of type int holds the pet's age.
The Pet class should also have the following methods:
constructor for this class. The constructor should accept an argument for each of the fields.
setName, The setName method stores a value in the name field.
setAnimal. The setAnimal method stores a value in the animal field.
setAge. The setAge method stores a value in the age field.
getName. The getName method returns the value of the name field.
getAnimal. The getAnimal method returns the value of the animal field.
getAge. The getAge method returns the value of the age field.

public class Pet
{
    private String name;
    private String animal;
    private int age;

    public Pet(String name, String animal, int age)
    {
        this.name = name;
        this.animal = animal;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAnimal()
    {
        return animal;
    }

    public void setAnimal(String animal)
    {
        this.animal = animal;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}

Question 3
Car ClassCreate a new class named Car that has the following fields:
year - The year field is an int that holds a car's year model (e.g. 2015)
make - The make field is a String object that holds the make of the car (e.g. "Honda")
speed - The speed field is an double that holds a car's maximum speed (e.g. 85.0)
In addition, the Car class should have the following methods.
Constructor - The constructor should accept the car's year, make, and beginning speed as arguments
These values should be used to initialize the Car's year, make, and speed fields
Getter Methods - Write three accessor (getter) methods to get the values stored in an object's fields getYear(), getMake(), getSpeed()

public class Car
{
    private int year;
    private String make;
    private double speed;

    public Car(int year, String make, double speed)
    {
        this.year = year;
        this.make = make;
        this.speed = speed;
    }

    public int getYear()
    {
        return year;
    }

    public String getMake()
    {
        return make;
    }

    public double getSpeed()
    {
        return speed;
    }
}

Question 4
Consider the following definition of the class Sample:
public class Sample 
{
    private int x;
    private double y;

    public Sample() 
    {
    }

    public Sample(int a, double b)
    {
    }

    public void setSample()
    {
    }

    public void print()
    {
    }
}
a. How many constructors does class Sample have?
b. Write the definition of the member setSample so that x is set to 10 and y is set to 25.8.
public void setSample()
{
    x = 10;
    y = 25.8;
}================================================================================
c. Write the definition of the member print that prints the contents of x and y.
public void print()
{
    System.out.println("x = " + x + "y = " + y);
}
 d. Write the definition of the default constructor of the class Sample so that the instance variables are initialized to 0.
public Sample() 
{
    x = 0;
    y = 0;
}
 e. Write the definition of the constructor with parameters of the class Sample so that the instance variable x is initialized to the value of a and the instance variable y is initialized to the value of b.
public Sample(int a, double b) 
{
    x = a;
    y = b;
}
 f. Write a Java statement that creates the Sample object s and initializes the instance variables of s to 20 and 35.0, respectively.
Sample s = new Sample(20, 35.0);
 g. Write a Java statement that prints the values of the instance variables of s.
s.print();

Java Interview Questions

CoreJava:☒
1.What is static?
2.What is interface?
3.What is abstract class?
4.What is the difference between interface and abstract class?
5.Difference between HashMap and Hashtable?
6.Difference between List and Set?
7.Can we create object for abstract class?if not, why?
8.Whether we can declare class as static?
9.Polymorphism w.r.t variables and methods? Or overriding and overloading?
10.In what way we can sort the elements(default and custom sort) present in array and any of the collection objects?
11.How to generate random number in java?And also generate random numbers of format 'A23BC'?
12.Is it possible to call a protected data members of one package in another package? Similarly all other access modifiers and its data members scope from other packages?
13.What is thread?
14.Pre-increment/decrement and post-increment/decrement 
15.What is singleton class?
16.Explain List
17.Explain Set
18.Explain Map
19.Why strings are immutable?
20.What is exception?And its types?
21.What is checked and unchecked exception?
22.What is comparable and comparator?
23.All oops concepts in single program.Explain OOPS

☒Coding:☒
1.Write a java program
2.Print from 1 to 10 without using any loop
3.Reverse a string without using built in methods
4.Reverse each words in a sentence(string)without changing the order of words
5.Reverse a string without using any loop or built in function
6.Remove spaces from string without using replace()
7.Print occurrences of distinct letters present in string
8.Print the indexes of specificed letter in string
9.Print the returned indexes of 2 elements from an array[2,4,3,6,8,7] where addition of 2 elements is equal to target sum=7
10.Transpose a matrix using 2D array concept
11.Print the first repeated character from a string
Ex: abcdcba , output:c
12.Find whether the string is having unique characters or not
13.Write a program to remove duplicates from an array without using collection? (Note: new array shld have its size equal to unique number of elements present in given array)
14.Write a program to print fibonacci series
15.Find and print the maximum character present in string

✴️Core Java Concepts:
1. What are the main principles of Object-Oriented Programming (OOP)?
2. Explain the difference between == and equals() method in Java.
3. What is a ClassLoader in Java?
4. What is the difference between an abstract class and an interface?
5. What is a singleton class and how do you implement it?

✴️DataStructures Basic
1. How do you implement a linked list in Java?
2. Describe how a HashMap works in Java.
3. Write a program to reverse a string in Java.
4. How would you find the maximum value in an array?
5. Explain the concept of a binary search tree and write code to perform an in-order traversal.

✴️Multithreading and Concurrency
1. What is the difference between Runnable and Callable in Java?
2. Explain the synchronized keyword and its use cases.
3. What are the different ways to create a thread in Java?
4. What is a deadlock and how can it be prevented?
5. What is the volatile keyword in Java?

✴️Java Collections Framework
1. What is the difference between ArrayList and LinkedList?
2. How does a HashSet store elements (or any collection)?
3. Explain the differences between HashMap and TreeMap.
4. How would you sort a list of objects in Java?
5. What is the purpose of the ConcurrentHashMap class?

✴️Exception Handling
1. What are the different types of exceptions in Java?
2. Explain the use of try-catch-finally blocks.
3. How do you create a custom exception in Java?
4. What is the difference between throw and throws?
5. What is the try-with-resources statement in Java?

✴️Java 8 Features
1. What are lambda expressions in Java? Provide an example.
2. Explain the Stream API in Java 8.
3. What are default methods in interfaces?
4. What is a functional interface?
5. How do you use the Optional class?

✴️Design Patterns
1. What is the Singleton pattern and how do you implement it in Java?
2. Explain the Factory pattern with an example.
3. What is the Observer pattern and where is it used?
4. Describe the Decorator pattern and its use case.
5. What is the difference between the Proxy and Adapter patterns?

✴️Database Connectivity
1. How do you connect to a database using JDBC in Java?
2. What is a JDBC driver and how does it work?
3. Explain the difference between Statement and PreparedStatement in JDBC.
4. How do you handle transactions in JDBC?
5. What is the use of ResultSet in JDBC?