UrbanPro
true

Learn Advanced Java coaching from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Tricks in Java

P K Subudhi
14/05/2017 0 0
  1. Find out the compatible and incompatible assignments

class Qs1

{

     public static void main(String arg[])

     {

           /* PART 1

           int a=2.7f;     // not ok

           int b=2.7;      // not ok

           float c=5;      // ok

           float d=2.6;    // not ok

           byte e=5;       // ok

           int f=5;        // ok

           byte g=f;       // not ok

           */

           /* PART 2

           char a='a';     // ok

           char b=65535;   // ok

           byte c=65;      // ok

           char d=c;       // not ok

           boolean e=0;    // not ok

           double f=50L;   // ok

           **/

     }

}

  1. Can we define the class like this and what would be its output?

class Qs2

{

     public static void main(String arg[])

     {

           main();

           System.out.println("I am in main");

     }

     static void main()

     {

           System.out.println("I am in mini main");

     }

}

Ans: Yes. There would be no error. Output is

I am in mini main

I am in main

 

  1. Is it correct to execute this program like this? What would be its output?

class Qs3

{

     public static void main(String arg[])

     {

           String str[]={"Hay","Hello", "hi"};

           System.out.println("I am in main");

           main(str);

     }

}

Ans: Yes. It displays all correct way like a recursive function but after certain time it stops function when stack overflows

 

  1. Let the following classes are defined in a single program Qs4.java. What would the o/p if executed?

class Abc

{

     public static void main(String arg[])

     {

           System.out.println("I am in Abc main");

     }

}

class Xyz

{

     public static void main(String arg[])

     {

           System.out.println("I am in Xyz main");

     }

}

class Qs4

{

     public static void main(String arg[])

     {

           System.out.println("I am in main");

     }

}

Ans: The above code is absolutely valid. When we compile the program, system creates 3 .class files based on 3 classes (as defined in the program given above). While executing we have to use only one class as the start-up. So, system displays the code defined within its own main() method.

 

That is: if the code is executed as

                java Qs4

Output would be

                I am in main

But if it is executed as

                java Abc

Then the output would be

                I am in Abc main

 

  1. What would be the output to the following code?

class Abc

{

     public static void main(String arg[])

     {

           System.out.println(arg[0]);

     }

}

class Xyz

{

     public static void main(String arg[])

     {

           System.out.println(arg[1]);

     }

}

class Qs5

{

     public static void main(String arg[])

     {

           String str[]={"Ni","Tini"};

           System.out.println("I am in main");

           Abc.main(str);

           Xyz.main(str);

     }

}

 

Ans:

I am in main

Ni

Tini

 

 

 

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Simple Algorithms - Fibonacci, Finding GCD & Finding LCM.
import java.util.Scanner; //0,1,1,2,3,5,8,13,21,34... //fun(n)=fun(n-1)+fun(n-2) where n>=2 /*fib(9)=fib(8)+fib(7) =fib(7)+fib(6)+fib(6)+fib(5) =fib(6)+fib(5)+fib(5)+fib(4)+fib(5)+fib(4)+fib(4)+fib(3) */ public...

What are the JSP implicit objects ?
JSP provides Nine implicit objects by default. They are as follows with corresponding Object Type: i) out --> JspWriter ii) request -->HttpServletRequest iii) response --> HttpServletResponse iv)...

Use of Service Locator Pattern
If we want to reuse the java code that should be the best approach w.r.t re-usability, maintanence and saving time to concentrate on our own businbess logic/requirement. In the similar approach many patterns...
M

Best way to learn any software Course
Hi First conform whether you are learning from a real time consultant. Get some Case Studies from the consultant and try to complete with the help of google not with consultant. Because in real time same situation will arise. Thank you

Java Concurrency Model - Future Task...
Callables and Future The FiutureTask is "A cancellable asynchronous computation. This class provides a base implementation of Future, with methods to start and cancel a computation, query to see if the...
X

Looking for Advanced Java coaching Classes?

The best tutors for Advanced Java coaching Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Advanced Java coaching with the Best Tutors

The best Tutors for Advanced Java coaching Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more