Java Quiz Entry Level. It has 5 java questions and in each question there is a piece of code. Take this quiz now and share your results.
Which method declarations can be inserted at line 1 without any problems?
Please select 3 correct answers
The rule is that you cannot have methods that create ambiguity for the compiler in a class. It is illegal for a class to have two methods having same name and having same type of input parameters in the same order. Name of the input variables and return type of the method are not looked into. 1. Option 1 is wrong because, then both the methods will be same (as their method name and the class/type and order of the input parameters will be same). So this amounts to duplicate method which is not allowed. As mentioned, name of the input parameters does not matter. Only the type of parameters and their order matters. 2. 2 is valid because the type of input parameters are different. So this is a different method and does not amount to duplication. 3 and 4 are valid for the same reason 5 is not valid because it leads to duplicate method(as their method name and the class/type and order of the input parameters will be same). Note that as mentioned in the comments, return type does not matter.
Which of the following statements will set the side of Square object referred by mysq to 20?
What will the following program print when run?
There are a couple of important concepts in this question: 1. Within an instance method, you can access the current object of the same class using 'this'. Therefore, when you access this.myValue, you are accessing the instance member myValue of the ChangeTest instance. 2. If you declare a local variable (or a method parameter) with the same name as the instance field name, the local variable "shadows" the member field. Ideally, you should be able to access the member field in the method directly by using the name of the member (in this example, myValue). However, because of shadowing, when you use myValue, it refers to the local variable instead of the instance field. In showTwo method, there are two variables accessible with the same name myValue. One is the method parameter and another is the member field of ChangeTest object. Ideally, you should be able to access the member field in the method directly by using the name myValue but in this case, the method parameter shadows the member field because it has the same name. So by doing this.myValue, you are changing the instance variable myValue by assigning it the value contained in local variable myValue, which is 200. So in the next line when you print ct.myValue, it prints 200. Now, in the showOne method also, there are two variables accessible with the same name myValue. One is the method parameter and another is the member field of ChangeTest object. So when you use myValue, you are actually using the method parameter instead of the member field. Therefore, when you do : myValue = myValue; you are actually assigning the value contained in method parameter myValue to itself. You are not changing the member field myValue. Hence, when you do System.out.println(ct.myValue); in the next line, it still prints 200.
What will the output be?
Since E2 is a sub class of E1, catch(E1 e) will be able to catch exceptions of class E2. Therefore, E1 is printed. Once the exception is caught the rest of the catch blocks at the same level (that is the ones associated with the same try block) are ignored. So E is not printed. finally is always executed (except in case of System.exit()), so Finally is also printed.
What will be the output?
Here, m1() is overloading for three different argument types. So when you call ot.m1(1.0), the one with argument of type double will be invoked.
Share your Results: