Java Classes & Objects Practice
Defining classes, creating objects, instance variables and methods.
1
Simple Class - Student
Easy
Create a class Student with fields name (String) and age (int). In main, read name and age, create a Student object, and print "Name: , Age: ".
Input Format
Two lines: name, then age.
Output Format
Name: , Age:
Sample Test Cases
|
Sample Input 1
Alice 18 |
Sample Output 1
Name: Alice, Age: 18 |
|
Sample Input 2
Bob 20 |
Sample Output 2
Name: Bob, Age: 20 |
Constraints
1
Explanation
Create a constructor or set fields directly, then print.
2
Class with Method
Easy
Create a class Rectangle with a method int area() that returns length * breadth. Read L and B, create a Rectangle object, call area(), and print the result.
Input Format
A single line with two integers L and B.
Output Format
A single integer: the area.
Sample Test Cases
|
Sample Input 1
4 5 |
Sample Output 1
20 |
|
Sample Input 2
7 3 |
Sample Output 2
21 |
Constraints
1
Explanation
Store fields and return the product in area().
3
Multiple Objects
Easy
Create a class Book with title (String) and pages (int). Read two books (title and pages each) and print both in the format " has pages", one per line.
Input Format
Four lines: title1, pages1, title2, pages2.
Output Format
Two lines: book info for each book.
Sample Test Cases
|
Sample Input 1
Java Basics 300 Python Basics 250 |
Sample Output 1
Java Basics has 300 pages Python Basics has 250 pages |
Constraints
1
Explanation
Create two Book objects and print their details.
4
Instance Variable Count
Easy
Create a class Counter with an instance variable int count (starts at 0) and a method void increment() that adds 1. Read N, call increment() N times, then print count.
Input Format
A single integer N.
Output Format
A single integer: the final count.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
5 |
|
Sample Input 2
0 |
Sample Output 2
0 |
Constraints
1
Explanation
Loop N times calling increment().
1
Constructor with Parameters
Medium
Create a class Circle with fields radius (double) and a constructor that takes radius. Add a method double area() that returns 3.14 * r * r. Read R, create the object via the constructor, and print the area as an integer rounded down.
Input Format
A single integer R.
Output Format
A single integer: floor(area).
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
78 |
|
Sample Input 2
10 |
Sample Output 2
314 |
Constraints
1
Explanation
Use (int)Math.floor(c.area()) when printing.
2
Getter and Setter
Medium
Create a class Person with private field name and public getter getName() and setter setName(String). Read a name, set it with the setter, and print it with the getter.
Input Format
A single line containing a name.
Output Format
The name printed by the getter.
Sample Test Cases
|
Sample Input 1
Alice |
Sample Output 1
Alice |
|
Sample Input 2
Rahul |
Sample Output 2
Rahul |
Constraints
1
Explanation
Encapsulate the field behind getter/setter.
3
This Keyword
Medium
Create a class Employee with fields name and salary, and a constructor that uses the this keyword to assign them. Read name and salary, create the object, and print "Name: , Salary: ".
Input Format
Two lines: name, then salary.
Output Format
Name: , Salary:
Sample Test Cases
|
Sample Input 1
Arjun 50000 |
Sample Output 1
Name: Arjun, Salary: 50000 |
Constraints
1
Explanation
Use this.name = name to resolve shadowing.
4
Static Field and Method
Medium
Create a class MathUtils with a static method int square(int n). Read N and call MathUtils.square(N) (no object needed), printing the result.
Input Format
A single integer N.
Output Format
A single integer: N*N.
Sample Test Cases
|
Sample Input 1
6 |
Sample Output 1
36 |
|
Sample Input 2
12 |
Sample Output 2
144 |
Constraints
1
Explanation
Call the static method using the class name.
5
Method Overloading (Same Class)
Medium
Create a class Calculator with overloaded methods: int add(int a, int b) and int add(int a, int b, int c). Read three integers X, Y, Z and print add(X,Y) on one line and add(X,Y,Z) on the next.
Input Format
A single line with three integers X, Y, Z.
Output Format
Two lines: add(X,Y), then add(X,Y,Z).
Sample Test Cases
|
Sample Input 1
2 3 4 |
Sample Output 1
5 9 |
|
Sample Input 2
-1 5 2 |
Sample Output 2
4 6 |
Constraints
-10^6
Explanation
Define both overloads and call each.
1
Array of Objects
Hard
Create a class Student with name (String) and marks (int). Read N and then N students (name and marks each). Print the name of the student with the highest marks. If tied, print the first one.
Input Format
First line: N. Next N lines: name and marks.
Output Format
The name of the topper.
Sample Test Cases
|
Sample Input 1
3 Alice 85 Bob 92 Carol 78 |
Sample Output 1
Bob |
Constraints
1
Explanation
Store Student[] and find the max marks.
2
Object as Parameter
Hard
Create a class Point with fields x and y. Write a method static double distance(Point a, Point b) that returns sqrt((a.x-b.x)^2 + (a.y-b.y)^2). Read four integers (x1 y1 x2 y2), create two Point objects, and print the distance rounded down to the nearest integer.
Input Format
A single line with four integers x1 y1 x2 y2.
Output Format
A single integer: floor(distance).
Sample Test Cases
|
Sample Input 1
0 0 3 4 |
Sample Output 1
5 |
|
Sample Input 2
1 1 4 5 |
Sample Output 2
5 |
Constraints
-1000
Explanation
Use Math.hypot or Math.sqrt.
3
Returning an Object
Hard
Create a class Time with hours and minutes. Write a method static Time add(Time t1, Time t2) that returns a new Time (carry minutes over 60 to hours). Read two times as "h m" each and print the sum as "h m".
Input Format
Two lines, each "h m".
Output Format
A single line: summed hours and minutes.
Sample Test Cases
|
Sample Input 1
1 30 2 45 |
Sample Output 1
4 15 |
|
Sample Input 2
5 50 6 20 |
Sample Output 2
12 10 |
Constraints
0
Explanation
Return a new Time object from add().
Competitive MCQs — Java Classes & Objects
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score
0/ 31
Q1
Which keyword creates an object from a class?
Correct!
Wrong — correct answer is .
new ClassName() allocates and initializes an instance.
Q2
What is the output of this code?
java
1
2
3
4
5
6
2
3
4
5
6
class Dog {
void bark() {
System.out.println("Woof");
}
}
new Dog().bark();Correct!
Wrong — correct answer is .
new Dog().bark() invokes the method, printing "Woof".
Q3
What is the output of this code?
class Dog {
void bark() {
System.out.println("Woof");
}
}
new Dog().bark();
class Dog {
void bark() {
System.out.println("Woof");
}
}
new Dog().bark();
Correct!
Wrong — correct answer is .
new Dog().bark() invokes the method, printing "Woof".
Q4
What is a constructor?
Correct!
Wrong — correct answer is .
Constructors run when an object is created with new.
Q5
What is the name of a constructor?
Correct!
Wrong — correct answer is .
A constructor must match the class name exactly.
Q6
What is the output of this code?
java
1
2
3
4
2
3
4
class A {
int x = 5;
}
System.out.println(new A().x);Correct!
Wrong — correct answer is .
The object's field x is 5.
Q7
What is the output of this code?
class A {
int x = 5;
}
System.out.println(new A().x);
class A {
int x = 5;
}
System.out.println(new A().x);
Correct!
Wrong — correct answer is .
The object's field x is 5.
Q8
What does the default constructor do?
Correct!
Wrong — correct answer is .
If no constructor is declared, Java supplies a no-arg default.
Q9
What is the output of this code?
java
1
2
3
4
5
6
7
2
3
4
5
6
7
class Point {
int x;
Point(int x) {
this.x = x;
}
}
System.out.println(new Point(7).x);Correct!
Wrong — correct answer is .
The constructor stores 7 into this.x.
Q10
What is the output of this code?
class Point {
int x;
Point(int x) {
this.x = x;
}
}
System.out.println(new Point(7).x);
class Point {
int x;
Point(int x) {
this.x = x;
}
}
System.out.println(new Point(7).x);
Correct!
Wrong — correct answer is .
The constructor stores 7 into this.x.
Q11
What is an object?
Correct!
Wrong — correct answer is .
Classes are blueprints; objects are their instances.
Q12
Which statement about instance fields is TRUE?
Correct!
Wrong — correct answer is .
Instance fields are per-object state.
Q13
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
class A {
static int count = 0;
A() {
count++;
}
}
new A();
new A();
System.out.println(A.count);Correct!
Wrong — correct answer is .
Two constructions increment the shared static count to 2.
Q14
What is the output of this code?
class A {
static int count = 0;
A() {
count++;
}
}
new A();
new A();
System.out.println(A.count);
class A {
static int count = 0;
A() {
count++;
}
}
new A();
new A();
System.out.println(A.count);
Correct!
Wrong — correct answer is .
Two constructions increment the shared static count to 2.
Q15
What is the output of this code?
class Student {
String name;
Student(String n) {
name = n;
}
}
System.out.println(new Student("Aya").name);
class Student {
String name;
Student(String n) {
name = n;
}
}
System.out.println(new Student("Aya").name);
Correct!
Wrong — correct answer is .
The constructor stores "Aya" in the name field.
Q16
What is the output of this code?
class A {
A() {
System.out.println("built");
}
}
new A();
class A {
A() {
System.out.println("built");
}
}
new A();
Correct!
Wrong — correct answer is .
Creating A() runs the constructor, printing "built".
Q17
What is the output of this code?
class A {
int x;
}
System.out.println(new A().x);
class A {
int x;
}
System.out.println(new A().x);
Correct!
Wrong — correct answer is .
Numeric fields default to 0.
Q18
What does new do?
Correct!
Wrong — correct answer is .
new reserves heap memory and invokes the constructor.
Q19
What is the output of this code?
class A {
int val = 7;
}
A a = new A();
System.out.println(a.val);
class A {
int val = 7;
}
A a = new A();
System.out.println(a.val);
Correct!
Wrong — correct answer is .
The instance field val holds 7.
Q20
Which statement is TRUE about instance fields?
Correct!
Wrong — correct answer is .
Instance fields belong to each individual object.
Q21
What is a field?
Correct!
Wrong — correct answer is .
Fields store the state of class instances (or the class).
Q22
What is the output of this code?
class A {
A() {
System.out.print("A");
}
}
class B extends A {
B() {
System.out.print("B");
}
}
new B();
class A {
A() {
System.out.print("A");
}
}
class B extends A {
B() {
System.out.print("B");
}
}
new B();
Correct!
Wrong — correct answer is .
The parent constructor runs first automatically, printing A then B.
Q23
Which keyword refers to the current object?
Correct!
Wrong — correct answer is .
this refers to the object whose method is executing.
Q24
What is the output of this code?
class A {
int x;
A(int x) {
this.x = x;
}
}
System.out.println(new A(5).x);
class A {
int x;
A(int x) {
this.x = x;
}
}
System.out.println(new A(5).x);
Correct!
Wrong — correct answer is .
this.x distinguishes the field from the parameter.
Q25
Which access modifier makes a field visible everywhere?
Correct!
Wrong — correct answer is .
public members are accessible from any class.
Q26
What is the output of this code?
class A {
static void hi() {
System.out.println("Hi");
}
}
A.hi();
class A {
static void hi() {
System.out.println("Hi");
}
}
A.hi();
Correct!
Wrong — correct answer is .
Static methods are called through the class name.
Q27
Can a class have multiple constructors?
Correct!
Wrong — correct answer is .
Constructors overload like other methods, differing by parameters.
Q28
What is the output of this code?
class A {
int x = 1;
int x() {
return 2;
}
}
System.out.println(new A().x());
class A {
int x = 1;
int x() {
return 2;
}
}
System.out.println(new A().x());
Correct!
Wrong — correct answer is .
The method x() returns 2; fields and methods can share names.
Q29
What is encapsulation at the class level?
Correct!
Wrong — correct answer is .
Private fields plus public methods is the classic encapsulated design.
Q30
What is the output of this code?
class A {
String name = "Default";
A(String name) {
this.name = name;
}
}
System.out.println(new A("Java").name);
class A {
String name = "Default";
A(String name) {
this.name = name;
}
}
System.out.println(new A("Java").name);
Correct!
Wrong — correct answer is .
The parameter overrides the initial value.
Q31
Which keyword creates a new object?
Correct!
Wrong — correct answer is .
ClassName ref = new ClassName(); is the standard object creation.