Class 12 IT 802 Unit 3 Fundamentals of Java – Important Questions Answers

Topics: Object-Oriented Programming and Class Design


Que 1. What are the fundamental features of Java?

Answer: Classes and Objects

Que 2. Java is an _______ programming language?

Answer: Object Oriented

Que 3. What is Class?

Answer: Class is a physical or logical entity that has certain attributes and behaviour. Attributes of class are known as Data Members and behaviour of the class is known as Method Members.

Que 4. What is an Object?

Answer: Object is an instance of a class, having its own attributes.

Que 5. Differentiate between class and object.

Answer: Class is a blueprint or template of the object. It is a logical entity. While the object is the instance of the class. It is a physical entity.

Que 6. Write the purpose of Data Members and Member Method.

Answer: Data Members are the attributes of objects, which are used to store the value.

Member methods of a class are invoked on an object to perform the action associated with that method.

Que 7. Create a class – Book contains the following data members and member methods.

Data Members – Title, Author, Publisher, Genre, Price.

Member Methods – display()

Answer: Class Book

public class Book {
    private String title;
    private String author;
    private String publisher;
    private String genre;
    private double price;
    
    void display(){
        System.out.println("Details of Book");
        System.out.println("Title : "+title);
        System.out.println("Author : "+author);
        System.out.println("Publisher : "+publisher);
        System.out.println("Genre : "+genre);
        System.out.println("Price : Rs."+price);
    }
   
    
}

Que 8. Which keyword is used to create a class in Java?

Answer: class

Que 9. How can you create a class in java? Explain.

Answer: A class in Java begins with the keyword class followed by the name of the class.

The body of the class is enclosed within curly braces.

The body contains the definitions of the data and method members of the class.

The data members are defined by specifying their type.

The method members of the class are defined just like the user defined methods we saw earlier.

The method members have access to all the data members of the class and can use them in their body.

Que 10. “The data members of a class are
like global variables”. What does it mean?

Answer: It means that the data members of the class are accessible by all the method members of the class.

Constructor

Que 11. By default data, members of the class are initialized with what value?

Answer: Zero or null.

A data member that is declared but not initialized before use, is assigned a default value by the compiler usually either zero or null.

Que 12. What is Constructor?

Answer: Constructor is a special method member of the class, which is used to initialize the data members of the class either by default value or specified value.

Que 13. What are the characteristics of the Constructor? or Why constructor is known as a Special method member?

Answer: Constructor has the following characteristics:-

  • The constructor has the same name as the class.
  • It has no return type.
  • The constructor may or may not have a parameter list.
  • The constructor of the class is invoked automatically whenever a new object of a class is created.
  • We do not call the constructor explicitly.

Que 14. How many types of Constructors?

Answer: In Java, there are two types of Constructors.

(a) Default Constructor and (b) Parameterized Constructor.

Que 15. What is Default Constructor?

Answer: The constructor, which is defined without any parameter is known as the default constructor. The default constructor is used to initialize the object’s data members (i.e. instance variables) with a default value.

Que 16. What is Parameterized Constructor?

Answer: The constructor having the parameter(s) is known as the parameterized constructor. The parameterized constructor is used to initialize the object’s data members (i.e. instance variables) with the given value.

Que 17. Define a class – Student with default constructor?

Answer: Class Student Definition with Default constructor.

class Student{
//  Data members

      private int admno;
      private String name;

// Constructor      

      Student(){   // default constructor
           admno = 9999;
           name = " ";
      }

// Method member

      void display(){
            System.out.println("Admission Number : "+admno);
            System.out.println("Name  : "+name);
}

Que 18. Define a class – Student with parameterized constructor?

Answer: Class Student Definition with Default constructor.

class Student{
//  Data members

      private int admno;
      private String name;

// Constructor 
     
      Student(int ad, String nm){   // parameterized constructor
           admno = ad;
           name = nm;
      }

// Method member

      void display(){
            System.out.println("Admission Number : "+admno);
            System.out.println("Name  : "+name);
}

Que 19. In the given code, which constructor is invoked in line-1?

class Student{
//  Data members

      private int admno;
      private String name;

// Method member

      void display(){
            System.out.println("Admission Number : "+admno);
            System.out.println("Name  : "+name);
 
     public static void main(String[] args){
            Student sd = new Student()    // Line 1
     }
}

Answer: Defualt constructor, which is created by java compiler automatically. This default constructor initialized the data members with java compiler default value i.e. either 0 or null.

Que 20. In the given code, Why Line – 1 is generate error message?

class Student{
//  Data members

      private int admno;
      private String name;

// Constructor 
     
      Student(int ad, String nm){   // parameterized constructor
           admno = ad;
           name = nm;
      }

// Method member

      void display(){
            System.out.println("Admission Number : "+admno);
            System.out.println("Name  : "+name);

     public static void main(String[] args){
            Student sd = new Student()    // Line 1

      }
}

Answer: Once a parameterized constructor has defined in the class, then we will not be able to
create an object without any parameters –

Student sd = new Student() // Line 1.

The compiler will complain that the methods actual and formal arguments differ in length.

Que 21. Which operator is used to access the data members and method member of an object?

Answer: . dot operator.

objectname.data_member

objectname.method_member();

Que 22. All Java Classes are inherited from which base class?

Answer: Object base class.

Que 23. Write the name of two method members name which are not crated by you in your class.

Answer: hashCode(), wait()

You cannot copy content of this page

Scroll to Top