Question 6

Print First N Prime numbers

N – range of prime numbers to be printed


#1 Get the input range

#2 Initialize the number with ‘2’(since prime number starts with 2)

#3 Loop within the input range

#4 Check whether the number is prime

#5 If not prime break the loop

#6 Else it is a prime add it to the output



Question 5

Check a given number is prime or not

Prime number: A number which is divisible by 1 and by itself.

Note: 1 is not a prime number. Prime number starts with ‘2’


#1 Get the input number

#2 Initialize the loop variable by 2 and continue till the square root of the number

#3 If the input number is exactly divisible by loop variable then its not a prime number, hence break the loop

#4 Else the given number is prime

Question 4

Remove the duplicate elements in an integer array


  1. Create an empty LinkedHashSet
  2. Loop(iterate) the input array till there are elements
  3. Add the iterated elements into the set
  4. End the loop
  5. Convert the set to an output array
  6. Print the output array


Input: {1,4,5,8,5,2}(Array)



Question 3

Vowel Count

Find the number of vowels in a given string.

Input: Onion

Output:3(hint:’O’ & ‘o’ are of different cases so count is 2 and ‘i’ count is 1. Hence total count is 3)

NOTE: case sensitive

Question 2

Vowel Count

Find the number of vowels in a given string irrespective of case.

Input: Onion

Output:2(hint:’O’ & ‘o’ denotes the same vowel, so count is 1 and ‘i’ count is 1. Hence total count is 2)

NOTE: irrespective of case, case insensitive, ignore case all the term holds the same meaning

Question 1

LCM – Least Common Multiple of both the numbers


  • Have any common multiple except 1
  • Always greater than or equal to the maximum number


#1.Find the maximum and minimum number

#2.Loop from 1 till the minimum number

#3.Find the multiple of the maximum number

#4.Check the multiple of maximum number is also a multiple of minimum number

#5.If the multiple is exactly divisible, then it is the LCM and return from the loop

#6.If the multiple is not exactly divisible, then continue with the loop


Ex: LCM(5,7)




max=7, min=5

for(int i=1;i<min;i++) multiple=i*max If(multiple%min==0) lcm=multiple
1;1<=5;1++ 1*7=7 7%5=2==0(false)
2;2<=5;2++ 2*7=14 14%5=4==0(false)
3;3<=5;3++ 3*7=21 21%5=1==0(false)
4;4<=5;4++ 4*7=28 28%5=3==0(false)
5;5<=5;5++ 5*7=35 35%5=0==0(true) lcm=35



Chapter 4 – Methods and Encapsulation

Access Modifiers

# public – any class

# private – same class only

# protected – same class, sub classes in the same and other package

# default – package private (within package)

Ex: default void walk(){}//does not compile

In the above line, default is not a valid access modifier

#default method is only allowed in interface

Optional Specifier

# can have zero or more access specifier

Ex: static, final, abstract, synchronized, native, strictfp


# Optional specifier and access specifier can be interchanged

# But return type and optional specifier cannot be interchanged


public void final dummy(){}//DOES NOT COMPILE

final public void dummy(){}//IT COMPILES

Return Type

# void – no return type, omit return or give return statement and without value.


public void dummy(){return;}

public String walk(int a){if(a==4) return ””;}//DOES NOT  COMPILE

Working with Varargs

# different than array

# a varargs parameter must be last element in method signature

# implies only one varargs parameter per method

# pass an array or list the elements of the array or omit the values (creates array of length zero)


package chapter4;

public class ex1 {

public static void walk(int start, int… nums) {



public static void main(String[] args) {


walk(1, 2);

walk(1, 2, 3);

walk(1, new int[] { 2, 4, 5 });

walk(1, null);








Exception in thread “main” java.lang.NullPointerException

at chapter4.ex1.walk(

at chapter4.ex1.main(

Static Methods

# main can be called like any other static methods

# static methods have two main purposes:

■ For utility or helper methods that don’t require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.

■ For state that is shared by all instances of a class, like a counter. All instances must share the same state. Methods that merely use that state should be static as well.

Static Variable

# when we update a final variable of primitive type, compiler error is thrown


package chapter4;

public class ex2 {

public static final int NUM_BUCKETS = 45;

public static void main(String[] args) {




# a final variable of reference type are allowed to call methods


package chapter4;

import java.util.ArrayList;

public class ex3 {

public static final ArrayList<String> values = new ArrayList<>();


public static void main(String[] args) {




Static Initialization

# static initializer runs when class is first used

# statements in it run and assign a static variable as needed.

Ways of initializing final variable

#1: Direct initialization

final float PI=3.14f; static final float PI=3.14f;

#2: Instance Initializer Block


package chapter4;

public class ex4 {

final float PI;


PI = 3.14f;



#3: Static Initializer Block


package chapter4;

public class ex5 {

static final float PI;

static {

PI = 3.14f;




package chapter4;

public class ex6 {

private static int one;

private static final int two;

private static final int three = 3;

private static final int four; // DOES NOT COMPILE

static {

one = 1;

two = 2;

three = 3; // DOES NOT COMPILE

two = 4; // DOES NOT COMPILE



Static Import

# its for importing static members.

Syntax: import static java.util.Arrays.asList;


#1 if you are importing static member, you should directly access static variable,


Arrays.asList(“one”);//DOES NOT COMPILE

asList(“one”);//IT COMPILES

#2 importing same name gives compiler error


import static statics.A.TYPE;

import static statics.B.TYPE; // DOES NOT COMPILE

Rules for method signature

#1 change in type/order/number of arguments

#2 return type not considered

#3 varargs overloading

Ex: both the lines are same,

public void fly(int[] args){}

public void fly(int… args){}

#4 Primitive level overloading follows the sequence,

  • Exact match
  • If exact match not found, then autoboxing
  • If autoboxing unavailable, then Object
  • If Object unavailable, convert to high level


# if constructor is private, we cannot create instance of the class in another class

# we cannot call constructor without new keyword

# can be called followed by new keyword


package chapter4;

public class ex8_2 {

public ex8_2(int weight) {

new ex8_2(weight, “brown”);


public ex8_2(int weight, String color) {




# this() must be the first statement in constructor