Search Placementyogi.com
 

Methods in Java Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

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

Inside Test()

x=0

B.

Compilation Error

C.

None of the Above

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

The signature of the method test() shows that it will return an integer, but it is not returning anything. Hence, Compiler throws the following error.

C:\java_practice\src\classes\MyClass.java:12: error: missing return statement

        }

        ^

1 error

 
2

What is the output of the following program?

class MyClass 
{
	public static void main(String[] args) 
	{
		int x;
		System.out.println("Begin Main");
		x=test();
		System.out.println("End Main");
	}
	static float test()
	{
		System.out.println("Inside Test()");
		return 10;
	}
}
A.

10

B.

10.0

C.

Compilation Error

D.

None of the Above

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

The compiler throws the following error:

C:\java_practice\src\classes\MyMethod.java:7: error: possible loss of precision

                x=test();

                      ^

  required: int

  found:    float

1 error

 
3

Can we initialize a variable like this?

class MyClass 
{
	public static void main(String[] args) 
	{
		int i=test();
		System.out.println("i="+i);
		
	}
	static int  test()
	{
		return 10;
		
	}
}
A. Yes
B. No
See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Yes, because the returned value from the method is used to initialize the variable. Since the method returns 10, i is assigned value 10 and the program executes correctly.

 
4

What is the output of the following program?

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

Inside Main()

Inside Test()

10

B.

Inside Main()

Inside Test()

C.

Compilation Error

D.

None of the Above

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

Even if the value returned by the method is not caught into any variable, the program works fine and the returned value is simply ignored.

 
5

What is the output of the following program?

class MyClass 
{
	public static void main(String[] args) 
	{

		System.out.println("www."+ test()+".com");
	}
	static void test()
	{
		System.out.println("placementyogi");
	}
}
A.

www.placementyogi.com

B.

www..com

C.

Compilation Error

D.

www.null.com

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

You cannot call a method which returns void from inside a System.out.println statement.

The following error mentions that clearly.

C:\java_practice\src\classes\MyClass.java:6: error: 'void' type not allowed here

                System.out.println("www."+ test()+".com");                                   ^

1 error

 
 

Banner to LogicGuns.com

Hide Page Information

Methods in Java Introduction Objective Questions and Answers

Methods in Java Objective Questions and Answers for interview

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

Why learn Methods in Java?

Methods are an integral part of Java, Methods and functions in Java are same and refer to each other. In this section you can learn and practice Methods 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 Methods in Java Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Methods 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 Methods in Java quiz questions with answers as PDF files and eBooks.

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

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

How to solve Methods in Java problems?

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