An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# HackerRank_JavaBasic_QNA
This is a repository which discusses about the question and answers of Java Basic Certificate

Q1) Write a Comparator class with the following overloaded compare methods:


  • 1.boolean compare(int a, int b):Return true if int a = int b otherwise return false.

  • 2.boolean compare(String a, String b):Return true if String a = String b otherwise return false.

  • 3.boolean compare(int[] a, int[] b):Return true if int[] a = int[] b otherwise return false.



    • 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

    ![download](https://user-images.githubusercontent.com/38869235/220115922-b30a5b75-fba7-4d50-a47e-a3eedc0e9cba.png)