Search Placementyogi.com
 

Tricky Questions on Methods in Java Objective with Answers

  :: Topics ::

1

What is the output of the following program? 

class MyMethod 
{
	static int func1()
	{
		System.out.println("Inside Func1");
		return 10;
	}
	static int func2()
	{
		System.out.println("Inside Func2");
		return func1();
	}	
	static int func3()
	{
		System.out.println("Inside Func3");
		return func1()+func2();
	}	
	public static void main(String[] args) 
	{

		System.out.println(func3());
	}
}
A.

Inside Func3

Inside Func1

Inside Func2

20

B.

Inside Func3

20

C.

Inside Func3

Inside Func1

Inside Func2

Inside Func1

20

D.

Compilation Error

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

Try tracing the program:

1. first func3 is called.

Console: "Inside Func3" is printed

2. Then func1()+func2() is executed which calls func1() first.

Console: "Inside Func1" is printed in the next line and 10 is returned. 

The expression becomes:

return 10 + func2()

3. Then func2() is called.

Console: "Inside Func2" is printed. and func1 is called. 

Console: "Inside Func1" is printed again and 10 is returned.

4. The return expression in func3() becomes 10 + 10

return 10+10 = return 20

5. This returned value 20 is printed finally.

Console: 20

So combining all the Console labeled line above we get the final output:

Inside Func3

Inside Func1

Inside Func2

Inside Func1

20

 
2

What is the output of the following program?

class MyMethod 
{
	static int func( int i)
	{
		return  i++;
	}
	
	public static void main(String[] args) 
	{
		int i=0;
		int j=i++ + i + func(i++) + ++i
			  + func(i++) + i + --i + func(i--) + i + i
			  + func(i--) + ++i;

		System.out.println(i);
		System.out.println(j);
	}
}
A.

2

24

B.

2

26

C.

3

25

D.

None of the Above

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

Trace the program yourself!! :-p

Just Kidding!

Here is the trace:

j= i++ + i + func(i++) + ++i

+ func(i++) + i + --i + func(i--) + i + i

+ func(i--) + ++i;

= 0 + 1 + func(1) + 3 

+ func(3) + 4 + 3 + func(3) + 2 +2 

+ func(2) + 2 ;

= 26

and i becomes 2.

 
3

What is the output of the following program?

class MyMethod 
{
	static int func( int i)
	{
		return  ++i;
	}
	
	public static void main(String[] args) 
	{
		int i=0;
		int j=i++ + i + func(i++) + ++i
			  + func(i++) + i + --i + func(i--) + i + i
			  + func(i--) + ++i;

		System.out.println(i);
		System.out.println(j);
	}
}
A.

2

30

B.

2

26

C.

3

26

D.

None of the Above

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

Trace this one yourself based on the previous questions's explanation.

 
4

What is the output of the following program?

class MyMethod 
{
	static int func1( int i)
	{
		return  ++i;
	}
	static int func2( int i)
	{
		return  func1(++i) + func1(i++);
	}
	
	public static void main(String[] args) 
	{
		int i=0;
		i= func1(i++) + i + func2(i++) + i + func2(++i) + i;
		System.out.println(i);
	}
}
A.

24

B.

23

C.

25

D.

22

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

Just remember 2 things:

func1(i) returns i increment by 1 i.e. i+1

func2(i) returns 

= func1(++i) + func1(i++)

= func1(++i) + func1(i) // i++ reduces to i because of post increment

=  func1(i+1) + func1(i+1) // notice, the post increment in the 2nd part, so i+1 is passed not i+2

= (i+2) + (i+2)

= 2* i + 4

Now use this formula to evaluate the expression !

i= func1(0) + 1 + func2(1)+ 2 + func2(3)+ 3

 = 1 + 1 + 6 + 2 +10 + 3 = 23

Output:

23

 
5

What is the output of the following program?

class MyMethod 
{
	static int func1( int i)
	{
		return  func2(i++);
	}
	static int func2( int i)
	{
		return  func3(i--) + func3(--i);
	}
	static int func3(int i)
	{
		return i++ + ++i + i;
	}
	public static void main(String[] args) 
	{
		int i;
		i=func1(i=0);
		System.out.println(i);
		System.out.println(func1(++i));
	}
}
A.

2

6

B.

2

20

C.

6

8

D.

8

20

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

Hmm!! This one looks tough!! Let's make it easy !

i=func1(i=0)

= func1(0)

= func2(0++) // post decrement! so 0 is passed to func2

= func2(0) 

= func3(0--)+ func3(--0)

= func3(0)+ func3(-2)   <-------------- ( expression1 )

// very very important.! notice that the decremented value is used in the second call to func3() !!

Consider the above expression as "expression1"

Now let's find out what func3 does.

func3(i)

= i++ + ++i + i

If i=0

func3(0)

= 0++ + ++i + i

= 0 + ++i + i ( now the value of i has become 1 because of increment operator )

= 0 + ++1 + i

= 0 + 2 + i (i =2 now)

= 0 + 2 + 2

= 4

So the final result of func3(i) = i + 2*(i+2) , we can use this formula in the next call of func3()

like this

func3(-2)

= i + 2*(i+2)

= -2 + 2*(-2+2)

= -2 + 2*(0)

= -2

or

We could have got the same result by the non formula methods also but to make it less complex , we derived the formula.

func3(-2)

= -2++ + ++i + i

= -2 + ++(-1) + i

= -2 + 0 + 0

= -2

Thus, using these values in "expression1"

func3(0)+ func3(-2)

= 4 + -2

= 2

Thus, we get 2.

 

Now , this value has been assigned to "i" in main().

the next call to func1 is func1(++i) = func1(++2)= func1(3).

Evaluate the value of func1(3) in the same way described above.

you will get 20.

Hence 20 is printed.

 
 

Banner to LogicGuns.com

Hide Page Information

Java Difficult Methods Multiple Choice Questions and Answers for Written Round Interview

Here you can find Java Difficult Methods Multiple Choice Questions with answers and explanation. We have a vast collection of Multiple Choice Questions on Difficult Methods in Java that will help you a lot!

Why learn Java Methods?

Difficult Methods are an integral part of Java and many Multiple Choice Questions in the written round are asked from this topic.In this section you can learn and practice Java Difficult Methods (Multiple Choice 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 Java Difficult Methods Multiple Choice Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java Difficult Methods Multiple Choice Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for Java Difficult Methods, We have quiz Multiple Choice Questions with answers to help you test yourself..

\n

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

Here you can find objective type Java Difficult Methods Multiple Choice Questions and answers for interview and entrance examination.Multiple Answers and true or false type Multiple Choice Questions are also provided.

How to solve Java Difficult Methods problems?

You can easily solve all kind of Multiple Choice Questions based on Java Difficult Methods by practicing the exercises (including shortcut Difficult Methods to solve problems) given below.