• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Constructors

Constructors

  • Categories Constructors, OOPS, Java

In this post we will cover Constructors in java. You can watch our videos on Revise Java Constructors in 5 minutes – Click Here 

Q. 1 What is constructer? When is it invoked?   Or Write two characteristics of a constructor. Give example.
Ans.

  • Constructor is a special method(or function) defined with the same name as of the class.
  • It has no return type, not even void.
  • It is automatically invoked wherever an object of that class is created.
  • // Example: A simple constructor.
class MyClass {
  int x;
  // Following is the constructor
  MyClass() {
     x = 0;
  }
}

Q. 2 What is the difference between Constructor and method/function?
Ans.

ConstructorMethod/function
1. It is always defined by the same name as of the class1. It is defined by any name except class name and keyword.
2. It has no return type not even void data type2. It must begin with either void or a valid return data type (such as int, float, char etc.).
3. It initializes the data members of the class.3. A method contains one or more than one valid Java statements as its body.
4. It can not be invoked/called like a method because a constructor automatically runs whenever an object of the class is created.4. A method must be invoked or called either by using object of the class or directly.

Q. 3  What are different types of constructors?
Ans. There are 3 different types of constructors

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

Q. 4 What is Java default constructor?
Ans. There are 2 types of Default constructor one provided by Java itself and one implemented by the programmer.

  • If a class does not have any constructor, the Java compiler will automatically provide one non-parameterized-constructor (or default-constructor) for the class in the byte code.
  • The default constructor initializes the member variables to 0.
  • The access modifier of the default constructor is the same as the class itself.
  • A programmer can give his own implementation of default constructor and in that case it overrides they system provided constructor.

Q. 5 What is the difference between parameterized and copy constructor?
Ans.

  • Parameterized constructor: When a constructor takes in parameters within the parenthesis it is known as parameterized constructor.
  • Copy constructor: Copy Constructor is a special type of constructor that takes same class as argument. Copy constructor is used to provide a copy of the specified object.
  • Example:
public class simply
{
    int a;
    char c;
    simply( ){                // Default Constructor
        a= 10;
    }
    simply(char n){       //  Parameterized Constructor
        c=n;
    }
    simply(simply s)      // Copy Constructor
    {
        int a= s.a;
    }
}

Q. 6 Explain the concept of constructor overloading with an example.
Ans. Presence of two or more constructors in a class is known as constructor overloading.
Example:

class stud
{
static int year;
int Roll_No;
  stud( ){         // constructor 1. This is default or non-parameterized constructor
      year= 2019;
  }
  stud( int n){    // constructor 2. This is a parameterized constructor
      Roll-No= n;
  }

}

Q. 7 What is this keyword in Java?
Ans. this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java.It stores the address of current calling object. It is also known as reference or pointer to current object.
Any instance method can be called as this.method() and any instance variable var as this.var.

public class Actor
{
   string lastName;
   public Actor(String lastName)
   {
       this.lastName = lastName;
   }
}

Q. 8 What is parameterized constructor with example?
Ans. When a constructor takes in parameters within the parenthesis it is known as parameterized constructor.
Example:

public class simply
{
   int roll_no;
   char grade;
   simply( ){                // Default Constructor
       roll_no= 10;
   }
   simply(char n){       //  Parameterized Constructor
       grade =n;
   }
}

Q. 9 What are temporary instance of a class and how it is created? Give one example also.
Ans.

  • It is an anonymous instance that is created to perform only one particular job within a single statement.
  • Temporary instances are created by calling or invoking constructor of a class using explicit method within the statement which performs some specific job.
  • Example: new Obj().Show();

Q. 10 Fill in the blanks

  1. A member function having the same name as that of the class name is called ____.
  2. A constructor has ____ return type.
  3. A constructor is used when ______ is created
  4. A constructor without any argument is known as ___________
  5. ____________ constructor creates objects by passing value to it.
  6. The ______ keyword refers to the current object.
  7. The argument of a copy constructor is always passed by __________.
  8. The constructor generated by the compiler is known as ___________.

Ans.

  1. A member function having the same name as that of the class name is called constructor.
  2. A constructor has return no
  3. A constructor is used when an object is created
  4. A constructor without any argument is known as non-parameterised constructor.
  5. Parameterised constructor creates objects by passing value to it.
  6. The this keyword refers to the current object.
  7. The argument of a copy constructor is always passed by reference.
  8. The constructor generated by the compiler is known as default constructor.

Q. 11 State True or False

  1. The compiler supplies a special constructor in a class that does not have any constructor.
  2. A constructor is not defined with any return type.
  3. Every class must have all types of constructors.
  4. A constructor is a member function of a class.
  5. Constructor is used to initialize the data members of a class.
  6. A constructor may have different name than the class name.
  7. A constructor is likely to be defined after the class declaration.
  8. Copy constructor copies functions from one object to another.

Ans.

  1. True
  2. True
  3. False
  4. True
  5. True
  6. False
  7. False
  8. False
  • Share:
author avatar
Simply Coding

Previous post

Loops in Java
June 17, 2021

Next post

Python Queue
June 17, 2021

You may also like

Questions on Encapsulation
Questions on Encapsulation
4 July, 2021
Questions on Library classes
Questions on Library classes
4 July, 2021
Questions on String 1
What is Java String?
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now