https://github.com/avinandanbose/hackerrank_javabasic_qna
This is a repository which discusses about the question and answers of Java Basic Certificate
https://github.com/avinandanbose/hackerrank_javabasic_qna
Last synced: 2 months ago
JSON representation
This is a repository which discusses about the question and answers of Java Basic Certificate
- Host: GitHub
- URL: https://github.com/avinandanbose/hackerrank_javabasic_qna
- Owner: AvinandanBose
- License: gpl-3.0
- Created: 2023-02-20T12:05:37.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-20T13:04:38.000Z (over 2 years ago)
- Last Synced: 2025-01-26T17:11:23.010Z (8 months ago)
- Size: 20.5 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HackerRank_JavaBasic_QNA
This is a repository which discusses about the question and answers of Java Basic CertificateQ1) Write a Comparator class with the following overloaded compare methods:
- 3.a)Arrays a and b are of equal length.
- 3.b)For each index i( where 0 < = i < a.length ) a [ i ] = b [ i ].
```
class Comparator{
//boolean compare(int a, int b):Return true if int a = int b otherwise return false.
boolean compare(int a, int b){
if(a == b){
return true;
}
return false;
}
//boolean compare(String a, String b):Return true if String a = String b otherwise return false.
boolean compare(String a, String b){
if(a == b){
return true;
}
return false;
}
+++++++++++++++++++++++++++++++
//boolean compare(int[] a, int[] b):Return true if int[] a = int[] b otherwise return false.
//Arrays a and b are of equal length.
//For each index i( where 0<=i
Q2) Given a bare class, Arithmetic write a method sum, that accepts an array as an argument. Overload it to the process an array of Integer or String types as follows: For an Integer array, return the sum of the elements and For a String array, concatenate string in order.
```
public class Arithmetic {
// Method to sum the elements of an Integer array
public static int sum(Integer[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i]; //Sum of Elements
}
return sum;
}
// Method to concatenate the elements of a String array
public static String sum(String[] arr) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]); //concatenate the elements of a String
}
return sb.toString();
}
}
```
Q3) Below is the Question, what will be the result ?
```
class A{
public int add(int a, int b){
return a+b;
}
public static void main(String[] args){
A a = new A();
short s= 9;
System.out.println(a.add(s,6))
}
}
```
Ans: 15 , Reason: The casting occurs here is Widening Casting ,In Widening Casting(byte -> short -> char -> int -> long -> float -> double) , hence int can hold a char value , short value and a byte value .
Cannot remember last three question.....😃
Certificate Link
