Search Placementyogi.com
 

Classes and Objects in Java Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

Note: The new keyword is missing in the object creation! Will the following code work?

class MyClass 
{
	int i;
	float j;
	public static void main(String[] args) 
	{
		MyClass myObj= MyClass();
		System.out.println("myObj.i="+myObj.i+", myObj.j="+myObj.j);
	}
}
A.

myObj.i=0, myObj.j=0

B.

myObj.i=0, myObj.j=0.0

C.

Compilation Error

D.

Run Time Error

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

new is missing

Correct Syntax to create an object is:

MyClass myObj= new MyClass();

Error reported by Compiler is a bit confusing and does not say anything about it.

C:\java_practice\src\classes\MyClass.java:9: error: cannot find symbol

                MyClass myObj= MyClass();

                               ^

  symbol:   method MyClass()

  location: class MyClass

1 error

 
2

What is the output of the following program?

class MyClass 
{
	int i;
	float j;
	MyClass(int x, float y)
	{
		i=x;
		j=y;
	}

	public static void main(String[] args) 
	{
		MyClass myObj= new MyClass();
		System.out.println("myObj.i="+myObj.i+", myObj.j="+myObj.j);
	}
}
A.

myObj.i=0, myObj.j=0.0

B.

Compile Time Error

C.

Run Time Error

D.

Garbage Value

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Here we have overwritten the default constructor. So we will have to write a default constructor too as the object creatin is through default constructor.

MyClass.java:13: error: constructor MyClass in class MyClass cannot be applied to given types;

                MyClass myObj= new MyClass();

                               ^

  required: int,float

  found: no arguments

  reason: actual and formal argument lists differ in length

1 error

 
3

What is the output of the following program?

class MyClass 
{
	int i;
	float j;
	MyClass(int x, float y)
	{
		i=x;
		j=y;
	}
	MyClass()
	{
	}
	public static void main(String[] args) 
	{
		MyClass myObj= new MyClass();
		System.out.println("myObj.i="+myObj.i+", myObj.j="+myObj.j);
	}
}
A.

myObj.i=0, myObj.j=0.0

B.

Compile Time Error because the Default Constructor should be written before parameterized Constructor

C.

Run Time Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

The position of the default constructor does not matter. So the program will execute and  the default value of the variables will be set depending on their data type.

myObj.i=0, myObj.j=0.0

 
4

What is the output of the following program?

class MyClass 
{
	int i;
	float j;
	MyClass(int x, float y)
	{
		i=x;
		j=y;
	}
	public static void main(String[] args) 
	{
		MyClass myObj= new MyClass(10,20);
		System.out.println("myObj.i="+myObj.i+", myObj.j="+myObj.j);
	}
}
A.

myObj.i=10, myObj.j=20.0

B.

Compile Time Error: Default Constructor is Missing

C.

None of the Above

D.

Run Time Error

See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

The default constructor is not needed if you are not creating objects without any parameters to constructor.

If in the above questions we had written:

MyClass myObj= new MyClass();

This would have thrown a compile time error.

 
5

What is the output of the following program?

class MyClass 
{
	public static void main(String[] args) 
	{
		System.out.println("Inside Main()");
		test();
	}
	void static test()
	{
		System.out.println("Inside Test()");
	}
}
A.

Inside Main()

Inside test()

B.

Compilation Error

C.

Run Time Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

The declaration of test() is not correct. The name should immediately be preceded by the return type of the method

C:\java_practice\src\classes\MyClass.java:8: error: <identifier> expected

         void static  test()

             ^

C:\java_practice\src\classes\MyClass.java:8: error: '(' expected

         void static  test()

              ^

C:\java_practice\src\classes\MyClass.java:8: error: invalid method declaration;

return type required

         void static  test()

                      ^

3 errors

 
 

Banner to LogicGuns.com

Hide Page Information

Core Classes and Objects in Java Objective Questions and Answers

Here you can find Classes and Objects in Java interview questions with answers and explanation.

Why learn Classes and Objects in Java ?

In this section you can learn and practice Classes and Objects in Java (Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get Classes and Objects in Java Multiple Choice Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Classes and Objects in Java Multiple Choice Questions and Answers with explanation.

Where can I get Classes and Objects in Java Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Classes and Objects in Java questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand. All students, freshers can download Classes and Objects in Java quiz questions with answers as PDF files and eBooks.

Where can I get Core Classes and Objects in Java Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type Classes and Objects in Java questions and answers for interview and entrance examination. Multiple choice and true or false type questions are also provided.

How to solve Core Classes and Objects in Java problems?

You can easily solve all kind of questions based on Core Classes and Objects in Java by practicing the exercises (including shortcut methods to solve problems) given below.