Questions #Tagged [java]

Questions: 11

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    

How to put validations for MultipartFile in Spring Boot?

Using below code we can put validations for MultipartFile in Spring Boot:<br> <pre><xmp>private void validateExtension(MultipartFile file) { String extension = FilenameUtils.getExtension(file.getOriginalFilename()); if (!"png".equals(extension) && !"jpeg".equals(extension) && !"jpg".equals(extension)) { throw new InvalidFileExtensionException("Only jpg/jpeg and png files are accepted"); } } private void validateFileSize(MultipartFile file){ if (file.getSize() >= MAXIMUM_FILE_SIZE_ALLOWED) { throw new BusinessException("File size cannot be greater than 5 Mb"); } }</xmp></pre>


#java     #spring-boot     #spring    

How to convert simple Java project into maven project in eclipse?

Follow below steps to convert simple Java project into maven project in eclipse:<br> &#9679 Create simple Java project in eclipse.<br> &#9679 Right click on the project and go to Configure.<br> &#9679 Select option Convert to Maven project. This will create pom.xml.


#java     #pom.xml     #maven     #eclipse    

What is lazy loading concept in hibernate?

Lazy loading in hibernate is also known as dynamic function loading in which the components of a program would not be loaded into storage by default when a program is started. Ordinarily,the system loader automatically loads the initial program and all of its dependent components at the same time. In lazy loading concept, the dependents are only loaded as they are specifically requested. Lazy loading improve the performance of a program if most of the dependent components are never actually used or rarely used. Lazy loading can be achieved by setting lazy="false" in .hbm.xml file.


#java     #hibernate    

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(; } </pre>


#java     #exception     #core-java