Light bulbDark Mode is experimental, we are looking forward for collaborations and feedback. Show intent to code in One Psych

Light bulbDark Mode is experimental, we are looking forward for collaborations and feedback. Show intent to code in One Psych

Introduction

Java is a widely used object-oriented programming language and software platform that runs on billions of devices, including notebook computers, mobile devices, gaming consoles, medical devices and many others. The rules and syntax of Java are based on the C and C++ languages

Review sheet

Click here to open the sheet in view only mode, please create a copy of the sheet an share the same with the stake holder involved in the recruitment process.

Module to be covered

  1. Why is Java a platform independent language?
    Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.
    The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it
  2. Difference between Heap and Stack Memory in java. And how java utilizes this.
    Stack memory is the portion of memory that was assigned to every individual program. And it was fixed. On the other hand, Heap memory is the portion that was not allocated to the java program but it will be available for use by the java program when it is required, mostly during the runtime of the program.
    Java Utilizes this memory as -
     -  When we write a java program then all the variables, methods, etc are stored in the stack memory.
     -   And when we create any object in the java program then that object was created in the heap memory. And it was referenced from the stack memory.
    
  3. What do you understand by an instance variable and a local variable?
    Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.
    All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.
    Example:
    class Athlete {
    public String athleteName;
    public double athleteSpeed;
    public int athleteAge;
    }
    

    Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.
    Example:
    public void athlete() {
    String athleteName;
    double athleteSpeed;
    int athleteAge;
    }
    

Microservices Question

  1. Write main features of Microservices.?
    • Decoupling: Within a system, services are largely decoupled. The application as a whole can therefore be easily constructed, altered, and scalable
    • Componentization: Microservices are viewed as independent components that can easily be exchanged or upgraded
    • Business Capabilities: Microservices are relatively simple and only focus on one service
    • Team autonomy: Each developer works independently of each other, allowing for a faster project timeline
    • Continuous Delivery: Enables frequent software releases through systematic automation of software development, testing, and approval
    • Responsibility: Microservices are not focused on applications as projects. Rather, they see applications as products they are responsible for
    • Decentralized Governance: Choosing the right tool according to the job is the goal. Developers can choose the best tools to solve their problems
    • Agility: Microservices facilitate agile development. It is possible to create new features quickly and discard them again at any time.
  2. Write main components of Microservices.
    Some of the main components of microservices include:
    • Containers, Clustering, and Orchestration
    • IaC Infrastructure as Code Conception
    • Cloud Infrastructure
    • API Gateway
    • Enterprise Service Bus
    • Service Delivery
  3. What are the benefits and drawbacks of Microservices?
    Benefits:
    • Self-contained, and independent deployment module.
    • Independently managed services.
    • In order to improve performance, the demand service can be deployed on multiple servers.
    • It is easier to test and has fewer dependencies.
    • A greater degree of scalability and agility.
    • Simplicity in debugging & maintenance.
    • Better communication between developers and business users.
    • Development teams of a smaller size.

    Drawbacks:
    • Due to the complexity of the architecture, testing and monitoring are more difficult.
    • Lacks the proper corporate culture for it to work.
    • Pre-planning is essential.
    • Complex development.
    • Requires a cultural shift.
    • Expensive compared to monoliths.
    • Security implications.
    • Maintaining the network is more difficult.

Kafka Question

  1. What are some of the features of Kafka? Following are the key features of Kafka:-
    • Kafka is a messaging system built for high throughput and fault tolerance.
    • Kafka has a built-in patriation system known as a Topic.
    • Kafka Includes a replication feature as well.
    • Kafka provides a queue that can handle large amounts of data and move messages from one sender to another.
    • Kafka can also save the messages to storage and replicate them across the cluster.
    • For coordination and synchronization with other services, Kafka collaborates with Zookeeper.
    • Apache Spark is well supported by Kafka.
  2. What are the major components of Kafka?
    Following are the major components of Kafka:-
    • Topic:
      • A Topic is a category or feed in which records are saved and published.
      • Topics are used to organize all of Kafka's records. Consumer apps read data from topics, whereas producer applications write data to them. Records published to the cluster remain in the cluster for the duration of a configurable retention period.
      • Kafka keeps records in the log, and it's up to the consumers to keep track of where they are in the log (the "offset"). As messages are read, a consumer typically advances the offset in a linear fashion. The consumer, on the other hand, is in charge of the position, as he or she can consume messages in any order. When reprocessing records, for example, a consumer can reset to an older offset.
  • Producer:
    • A Kafka producer is a data source for one or more Kafka topics that optimizes, writes, and publishes messages. Partitioning allows Kafka producers to serialize, compress, and load balance data among brokers.
  • Consumer:
    • Data is read by consumers by reading messages from topics to which they have subscribed. Consumers will be divided into groups. Each consumer in a consumer group will be responsible for reading a subset of the partitions of each subject to which they have subscribed.
  • Broker:
    • A Kafka broker is a server that works as part of a Kafka cluster (in other words, a Kafka cluster is made up of a number of brokers). Multiple brokers typically work together to build a Kafka cluster, which provides load balancing, reliable redundancy, and failover. The cluster is managed and coordinated by brokers using Apache ZooKeeper. Without sacrificing performance, each broker instance can handle read and write volumes of hundreds of thousands per second (and gigabytes of messages). Each broker has its own ID and can be in charge of one or more topic log divisions.
    • ZooKeeper is also used by Kafka brokers for leader elections, in which a broker is chosen to lead the handling of client requests for a certain partition of a topic. Connecting to any broker will bring a client up to speed with the entire Kafka cluster. A minimum of three brokers should be used to achieve reliable failover; the higher the number of brokers, the more reliable the failover.
  1. Explain the four core API architecture that Kafka uses.
    • Producer API:
      The Producer API in Kafka allows an application to publish a stream of records to one or more Kafka topics.
    • Consumer API:
      An application can subscribe to one or more Kafka topics using the Kafka Consumer API. It also enables the application to process streams of records generated in relation to such topics.
    • Streams API:
      The Kafka Streams API allows an application to use a stream processing architecture to process data in Kafka. An application can use this API to take input streams from one or more topics, process them using streams operations, and generate output streams to transmit to one or more topics. The Streams API allows you to convert input streams into output streams in this manner.
    • Connect API:
      The Kafka Connector API connects Kafka topics to applications. This opens up possibilities for constructing and managing the operations of producers and consumers, as well as establishing reusable links between these solutions. A connector, for example, may capture all database updates and ensure that they are made available in a Kafka topic.

SpringBoot Question

  1. What are the advantages of Spring Boot?
    • Create stand-alone Spring applications that can be started using java -jar.
    • Embed Tomcat, Jetty or Undertow directly. You don't need to deploy WAR files.
    • It provides opinionated 'starter' POMs to simplify your Maven configuration.
    • It automatically configure Spring whenever possible.
  2. How to create Spring Boot application using Maven?
    There are multiple approaches to create Spring Boot project. We can use any of the following approach to create application.
    • Spring Maven Project
    • Spring Starter Project Wizard
    • Spring Initializr
    • Spring Boot CLI
  3. What are the Spring Boot Starters? Starters are a set of convenient dependency descriptors which we can include in our application.
    Spring Boot provides built-in starters which makes development easier and rapid. For example, if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project.

Technical Question

  1. How do you reverse a string in Java?
    There is no reverse() utility method in the String class. However, you can create a character array from the string and then iterate it from the end to the start. You can append the characters to a string builder and finally return the reversed string.
    public class StringPrograms {
    
    public static void main(String[] args) {
       String str = "123";
    
       System.out.println(reverse(str));
    }
    
    public static String reverse(String in) {
       if (in == null)
        throw new IllegalArgumentException("Null is not valid input");
    
       StringBuilder out = new StringBuilder();
    
       char[] chars = in.toCharArray();
    
       for (int i = chars.length - 1; i >= 0; i--)
        out.append(chars[i]);
    
       return out.toString();
    }
    
    }
    

    us points for adding null check in the method and using StringBuilder for appending the characters. Note that the indexing in Java starts from 0, so you need to start at chars.length - 1 in the for loop.
  2. How do you swap two numbers without using a third variable in Java? Swapping numbers without using a third variable is a three-step process that’s better visualized in code:
b = b + a; // now b is sum of both the numbers
a = b - a; // b - a = (b + a) - a = b (a is swapped)
b = b - a; // (b + a) - b = a (b is swapped)

The following example code shows one way to implement the number swap method:

public class SwapNumbers {

public static void main(String[] args) {
    int a = 10;
    int b = 20;

    System.out.println("a is " + a + " and b is " + b);

    a = a + b;
    b = a - b;
    a = a - b;

    System.out.println("After swapping, a is " + a + " and b is " + b);
    }

}

The output shows that the integer values are swapped:

Output
a is 10 and b is 20
After swapping, a is 20 and b is 10
  1. Write a Java program to check if a vowel is present in a string.
    public class StringContainsVowels {
    
    public static void main(String[] args) {
       System.out.println(stringContainsVowels("Hello")); // true
       System.out.println(stringContainsVowels("TV")); // false
    }
    
    public static boolean stringContainsVowels(String input) {
       return input.toLowerCase().matches(".*[aeiou].*");
    }
    

} ```

  1. Write a Java program to check if the given number is a prime number. You can write a program to divide the given number n, by a number from 2 to n/2 and check the remainder. If the remainder is 0, then it’s not a prime number. The following example code shows one way to check if a given number is a Prime number:
    public class PrimeNumberCheck {
    
    public static void main(String[] args) {
       System.out.println(isPrime(19)); // true
       System.out.println(isPrime(49)); // false
    }
    
    public static boolean isPrime(int n) {
       if (n == 0 || n == 1) {
        return false;
       }
       if (n == 2) {
        return true;
       }
       for (int i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            return false;
        }
       }
    
       return true;
    }
    
    }
    
  2. Write a Java program to print a Fibonacci sequence using recursion. A Fibonacci sequence is one in which each number is the sum of the two previous numbers. In this example, the sequence begins with 0 and 1. The following example code shows how to use a for loop to print a Fibonacci sequence:
    public class PrintFibonacci {
    
    public static void printFibonacciSequence(int count) {
       int a = 0;
       int b = 1;
       int c = 1;
    
       for (int i = 1; i <= count; i++) {
        System.out.print(a + ", ");
    
           a = b;
        b = c;
        c = a + b;
       }
    }
    
    public static void main(String[] args) {
       printFibonacciSequence(10);
    }
    
    }
    
    Output
    0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
    

    You can also use recursion to print a Fibonacci sequence, because the Fibonacci number is generated by adding the previous two numbers in the sequence:
    F(N) = F(N-1) + F(N-2)
    ```
    The following example class shows how to use recursion to calculate a Fibonacci sequence that is 10 numbers long:
    ```
    public class PrintFibonacciRecursive {

        public static int fibonacci(int count) {
            if (count <= 1)
                return count;

            return fibonacci(count - 1) + fibonacci(count - 2);
        }

        public static void main(String args[]) {
            int seqLength = 10;

            System.out.print("A Fibonacci sequence of " + seqLength + " numbers: ");

            for (int i = 0; i < seqLength; i++) {
                System.out.print(fibonacci(i) + " ");
            }
        }
    `}
    ```
Output
A Fibonacci sequence of 10 numbers: 0 1 1 2 3 5 8 13 21 34