• Slider Banner

    Prepare for

    Technical Interview

    Over .1 Million+ Interview Questions for different profiles

    Read More
  • Slider Banner

    Join the

    Programming World

    Learn about top programming languages, tools and frameworks.

    Read More
  • Slider Banner

    Prepare with

    Certification MCQ

    Practice for certification from 1000+ multiple choice questions.

    Read More

Given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. Both arrays will be length 1 or more.

Check Below Code:

package com.test.arrays;

public class CommonArrayEnd 
{
	public static void main(String args[])
	{
		CommonArrayEnd ca = new CommonArrayEnd();
		System.out.println(ca.commonEnd(new int[] {1, 2, 6}, new int[] {1, 2, 6}));
		System.out.println(ca.commonEnd(new int[] {1, 2, 3}, new int[] {7, 3, 2}));
		System.out.println(ca.commonEnd(new int[] {1, 2, 3}, new int[] {1}));
	}
	
	public boolean commonEnd(int[] a, int[] b) 
	{
		return (a[0] == b[0] || a[a.length-1] == b[b.length-1]);
	}
}

Output

true
false
true

Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".

Check below code:

package com.test.strings;

public class HelloName 
{
	public static void main(String args[])
	{
		HelloName hn = new HelloName();
		System.out.println(hn.helloName("Ram"));
		System.out.println(hn.helloName("Sham"));
		System.out.println(hn.helloName("X"));
	}
	
	public String helloName(String name) 
	{
		 return "Hello " + name + "!";
	}
}

Output

Hello Ram!
Hello Sham!
Hello X!

Given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.

Check below code:

package com.test.arrays;

public class FirstLastEqual 
{
	public static void main(String args[])
	{
		FirstLastEqual fl = new FirstLastEqual();
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 1}));
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 3, 1}));
		System.out.println(fl.sameFirstLast(new int[] {1, 2, 3}));
	}
	
	public boolean sameFirstLast(int[] nums) 
	{
		if(nums.length >= 1 && nums[0] == nums[nums.length-1])
			return true;
		else
			return false;	  
	}
}

Output

true
true
false

Given an array of ints, return true if 6 appears as either the first or last element in the array. The array will be length 1 or more.

Check Below Code:

package com.test.arrays;

public class SixFirstLast 
{
	public static void main(String args[])
	{
		SixFirstLast sf = new SixFirstLast();
		System.out.println(sf.firstLast6(new int[] {1, 2, 6}));
		System.out.println(sf.firstLast6(new int[] {2, 4, 6, 8}));
		System.out.println(sf.firstLast6(new int[] {6, 4, 6, 6}));
	}

	public boolean firstLast6(int[] nums) 
	{
		if(nums[0]==6 || nums[nums.length-1] == 6)
			return true;
		else
			return false;
	}
}	

Output

true
false
true

Given a string, return a version without the first and last char, so "Hello" yields "ell". The string length will be at least 2.

Check Below Code:

package com.test.strings;

public class StringFirstLast 
{
	public static void main(String args[])
	{
		StringFirstLast fl = new StringFirstLast();
		System.out.println(fl.firstLast("hello"));
		System.out.println(fl.firstLast("java"));
		System.out.println(fl.firstLast("coding"));
	}
		
	public String firstLast(String str) 
	{
		return str.substring(1, str.length()-1);	
	}
}

Output

ell
av
odin

Given a string of even length, return the first half. So the string "WooHoo" yields "Woo".

Check below code:

package com.test.strings;

public class FindStringFirstHalf 
{
	public static void main(String args[])
	{
		FindStringFirstHalf fh = new FindStringFirstHalf();
		System.out.println(fh.firstHalf("WooHoo"));
		System.out.println(fh.firstHalf("HelloThere"));
	}
	
	public String firstHalf(String str) 
	{
	  return str.substring(0, str.length()/2);	
	}
}

Output

Woo
Hello

What is object-oriented paradigm?

It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.

  • Follows the bottom-up approach in program design.
  • Focus on data with methods to operate upon the object's data
  • Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
  • Implements the real-time approach like inheritance, abstraction, etc.
  • The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.

What will be the output of below java code?

Example Code:

class ForWith0Comparison   
{
    public static void main (String args[])
    {
        for(int i=0; 0; i++)
        {
            System.out.println("Hello ProgramsBuzz");
        }  
    }  
} 

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to boolean

	at com.test.controlflowstatements.ForWith0Comparison.main(ForWith0Comparison.java:7)

What will be the output of the following Java program?

Example Code:

package com.test.strings;

class StringIntMultiply   
{
    public static void main (String args[])
    {
    	System.out.println(10 * 20 + "programsbuzz");
    	System.out.println("programsbuzz" + 10 * 20);  
    }  
}  

Output

200programsbuzz
programsbuzz200

Subscribe to