Questions #Tagged [core-java]

Questions: 8

What will be the output of the given Java static method related code snippet?

The answer is : <b>j=4</b><br> The lines to watch here are lines 9 & 10. Line 9 features the non-shortcut version of the OR operator so both of its operands will be evaluated and therefore methodB(4) is executed. However line 10 has the shortcut version of the OR operator and if the 1st of its operands evaluates to true (which in this case is true), then the 2nd operand isn't evaluated, so methodB(8) never gets called. The loop is only executed once, b is initialized to false and is assigned true on line 9. Thus j = 4.

 

#java     #core-java    

What will be the output of the given Java square root related code snippet?

The answer will be : <b>NaN</b><br> The sqrt() method returns NaN (not a number) when its argument is less than zero.

 

#java     #core-java    

What will be the output of the given Java unary operators related code snippet?

The answer will be : <b>i = 5 and j = 6</b><br> The prefix and postfix unary operators have a higher order of evaluation than the relational operators. So on line 4 the variable i is incremented and the variable j is decremented before the greater than comparison is made. The continue statement never gets to execute because i never reaches a value that is greater than j.

 

#java     #core-java    

What will be the output of the given Java code snippet?

The answer of this Java code snippet is xyzac. <br><br>Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no reference to it. Line 3 creates a new String object referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new String object, appends "ac" to the value "xyz", and refers y to the result.

 

#java     #core-java    

How many String objects will be created with the given Java code?

The answer is that there will be 5 String objects. Line 1 creates two, one referred to by x and the lost String "yz". Line 2 creates one (for a total of three). Line 3 creates one more(for a total of four). Line 4 creates one more (for a total of five), the concatenated String referred to by x with a value of "yzabcefg".

 

#java     #core-java    

Getting error code 403 when running spring-boot program? Is it something related to pom.xml?

403 error means Forbidden - you don't have permission to access / on this server. But in this case this issue is occurring due to Java version issue. Update your java version and move to open JDK to resolve this issue.

 

#java     #error     #core-java     #spring-boot     #pom.xml     #maven    

How to avoid ArrayIndexOutOfBoundsException in Java?

The ArrayIndexOutOfBoundsException in Java occurs whenever someone try to access any item of an array at an index which is not present in the array. In other words, the index may be negative or exceed the size of an array.<br><br> The ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundsException, and it implements the Serializable interface.<br><br> We can avoid this exception by using a type-safe Iterator. These are the iterators which allow modifications of a collection while iterating over it.<br><br> <pre>HashMap<Integer,Integer> map = new HashMap<>(); map.put(1,1); map.put(2,2); map.put(3,3); map.put(4,4); final Iterator<Integer> it = map.Keyset().iterator(); for (int i = 0; it.hasNext(); i++) { System.out.println(it.next()); } </pre>

 

#java     #exception     #core-java