logo codesdope

Practice questions on Constructor overloading

Level 1

1.

Write a program to print the names of students by creating a Student class. If no name is passed while creating an object of Student class, then the name should be "Unknown", otherwise the name should be equal to the String value passed while creating object of Student class.

class Student{
  String name;
  public Student(String s){
    name = s;
  }
  public Student(){
    name = "Unknown";
  }
}

class Ans{
  public static void main(String[] args){
    Student s = new Student("xyz");
    Student a = new Student();

    System.out.println(s.name);
    System.out.println(a.name);
  }
}

Level 2

Level 3

Close

Welcome.please sign up.

Close

Welcome.please login.