开发者

questions i tripped on in an interview [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

i was asked a few java interview questions which i had no idea how solve could use some answers

  1. write a method which takes an int so the method would be

    public void somemethd(int i){}

    Now if i=1 then it should print true and if i=2 it should print false but you cannot use any conditional operators nor can you use if or switch

  2. how do two threads pass data in between each other, like let lets say you start 2 threads and you want to make one thread make the other thread wait. he rephrased the question to make it simpler, what if i have a varible in 1 thread and i want to send it to another thread how to do that.

  3. what if there is a non static method in base class and you make it a static method in derived class, would you get the same error as if u has a static method in base and u try to make it non static in derived class

  4. what is the use of overloading other than same name for different method? (i guess i should have said constructor overloading but it just didnt come to me, is there anything else)

  5. the syntax is not correct but u can jus assume everything is fine

    class base{

    show(){

    this.mymethod(); }

    mymethod(){ /... } }

    class derived extends base{

    show(){
    
    super.mymethod();
    }
    
    mymethod(){
    /...
    }
    }
    

now what happens when i do the following

a.

base b = new derived();

   b.show(); 

b.

derived d= new base();

   d.show();

c. what if there was no show method in base n i did

 base b = new derived();

   b.show(); 

d. what if there was no show method in base n i did

derived d= new base();开发者_如何学运维 d.show();


One solution to the updated 1)

void printIsOne(int n) {
   System.out.println("?,true,false".split(",")[n]);
}

2) You cannot pass local variables between threads. However you can pass a value from one thread to another and have it wait for the value. IMHO: The simplest way is to use a BlockingQueue.

3) You get different error in both cases, but basically you cannot change one to the other in a derived class.

4) backward compatilibity. You may have code, e.g. loads of test code, which calls a method with one set of args however you want to change it in some way without changing the existing code.

5) just try it (hint: some combinations get a compiler error)


write a method which takes an int and prints true if 1 and prints false if true but u cant use if or swith or any conditional operators

I'm guessing you mean true if 1 and false if 0, in which case you can do this:

void printValue(int value)
{
    String[] values = { "false", "true" };
    System.out.println(values[value]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜