Skip to main content

Top 20 Java Pattern Programs Asked in Interviews Questions (With Code using for loop & Output)

Top 20 Java Pattern Questions Asked in Interviews

Top 20 Java Pattern Questions Asked in Interviews

Here are 20 frequently asked Java pattern program questions with their code and expected output. Use them for coding rounds or practice.


1. Right-Angled Triangle

* * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

2. Inverted Right-Angled Triangle

* * * * * * * * * * * * * * *
for(int i = 5; i >= 1; i--) {
    for(int j = 1; j <= i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

3. Pyramid Pattern

* *** ***** ******* *********
for(int i = 1; i <= 5; i++) {
    for(int j = i; j < 5; j++) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}

4. Diamond Pattern

* *** ***** ******* ********* ******* ***** *** *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = i; j < n; j++) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}
for(int i = n - 1; i >= 1; i--) {
    for(int j = n; j > i; j--) System.out.print(" ");
    for(int k = 1; k <= (2 * i - 1); k++) System.out.print("*");
    System.out.println();
}

5. Square Pattern

* * * * * * * * * * * * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

6. Hollow Square

* * * * * * * * * * * * * * * *
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= 5; j++) {
        if(i == 1 || i == 5 || j == 1 || j == 5) System.out.print("* ");
        else System.out.print("  ");
    }
    System.out.println();
}

7. Number Triangle

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}

8. Inverted Number Triangle

1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
for(int i = 5; i >= 1; i--) {
    for(int j = 1; j <= i; j++) {
        System.out.print(j + " ");
    }
    System.out.println();
}

9. Floyd’s Triangle

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int num = 1;
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(num++ + " ");
    }
    System.out.println();
}

10. 0-1 Triangle

1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print((i + j) % 2 + " ");
    }
    System.out.println();
}

11. Pascal’s Triangle

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
for(int i = 0; i < 5; i++) {
    int number = 1;
    for(int j = 0; j < 5 - i; j++)
        System.out.print(" ");
    for(int j = 0; j <= i; j++) {
        System.out.print(number + " ");
        number = number * (i - j) / (j + 1);
    }
    System.out.println();
}

12. Butterfly Pattern

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
int n = 4;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= i; j++) System.out.print("* ");
    for(int j = 1; j <= 2*(n-i); j++) System.out.print("  ");
    for(int j = 1; j <= i; j++) System.out.print("* ");
    System.out.println();
}
for(int i = n; i >= 1; i--) {
    for(int j = 1; j <= i; j++) System.out.print("* ");
    for(int j = 1; j <= 2*(n-i); j++) System.out.print("  ");
    for(int j = 1; j <= i; j++) System.out.print("* ");
    System.out.println();
}

13. Hollow Pyramid

* * * * * * * *********
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = i; j < n; j++) System.out.print(" ");
    for(int j = 1; j <= (2 * i - 1); j++) {
        if(j == 1 || j == (2 * i - 1) || i == n) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

14. Cross Pattern

* * * * * * * * *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n; j++) {
        if(j == i || j == (n - i + 1)) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

15. Right-Angled Number Triangle (Continuous)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int num = 1;
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(num++ + " ");
    }
    System.out.println();
}

16. Alphabet Triangle

A A B A B C A B C D A B C D E
for(int i = 1; i <= 5; i++) {
    for(char c = 'A'; c < 'A' + i; c++) {
        System.out.print(c + " ");
    }
    System.out.println();
}

17. Reverse Alphabet Triangle

E D C B A D C B A C B A B A A
for(int i = 5; i >= 1; i--) {
    for(char c = (char)('A' + i - 1); c >= 'A'; c--) {
        System.out.print(c + " ");
    }
    System.out.println();
}

18. Zig-Zag Star Pattern

* * * * * * * * *
int n = 9;
for(int i = 1; i <= 3; i++) {
    for(int j = 1; j <= n; j++) {
        if((i + j) % 4 == 0 || (i == 2 && j % 4 == 0)) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

19. Hollow Diamond

* * * * * * * * * * * * * * * *
int n = 5;
for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= n - i; j++) System.out.print(" ");
    for(int j = 1; j <= 2 * i - 1; j++) {
        if(j == 1 || j == 2 * i - 1) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}
for(int i = n - 1; i >= 1; i--) {
    for(int j = 1; j <= n - i; j++) System.out.print(" ");
    for(int j = 1; j <= 2 * i - 1; j++) {
        if(j == 1 || j == 2 * i - 1) System.out.print("*");
        else System.out.print(" ");
    }
    System.out.println();
}

20. Binary Triangle Pattern

1 0 1 1 0 1 0 1 0 1 1 0 1 0 1
for(int i = 1; i <= 5; i++) {
    for(int j = 1; j <= i; j++) {
        System.out.print(((i + j) % 2) + " ");
    }
    System.out.println();
}

🔁 Tip: Practice these patterns regularly and also dry-run the loops on paper to fully understand flow control. Bookmark this page for your coding interview prep!

📌 Note: More advanced pattern questions (like Pascal’s Triangle, Butterfly Pattern, and Zigzag) will be covered in part 2.

Comments

Popular posts from this blog

Top 15 React Interview Questions for 1–2 Years Experience

🟦 Top 15 React Interview Questions for 1–2 Years Experience Preparing for a React interview with 1–2 years of experience? Here's a carefully curated list of 15 important React questions with clear, real-world answers. These are frequently asked in interviews at companies like TCS, Infosys, Cognizant, Capgemini, and product-based firms. Q1. What is the Virtual DOM in React, and how does it improve performance? Answer: The Virtual DOM is a lightweight, in-memory copy of the real DOM. When state/props change, React creates a new Virtual DOM tree, compares it with the old one (diffing), and only updates the parts of the real DOM that changed. This makes rendering much faster and improves performance in large applications. Q2. What is JSX in React? Answer: JSX stands for JavaScript XML. It allows us to write HTML elements in JavaScript and place them in the DOM without using createElement() . JSX improves code readability and is transpiled to React.createElement() calls. ...

Top 15 Spring Boot Interview Questions and Answers – Real Examples (2025)

Top 15 Spring Boot Interview Questions – 2025 Spring Boot is one of the most demanded frameworks for Java backend development. Whether you're interviewing for TCS, Infosys, or a product-based company, these Spring Boot questions will help you prepare like a pro. Here are 15 questions with detailed explanations for developers with 1–2 years of experience. Q1. What is Spring Boot? Answer: Spring Boot is a Java-based open-source framework built on top of the Spring Framework. It helps developers create stand-alone, production-ready Spring applications with minimal configuration. Its key features include: Auto-configuration Embedded servers (Tomcat, Jetty) Starter dependencies Production-ready tools (Actuator, Metrics, etc.) Example: You can create a REST API within minutes by using @RestController and spring-boot-starter-web — no need for external web server deployment. Q2. What is the role of @SpringBootApplication annotation? Answer: This annotation i...

Wipro Java Developer Interview Questions with Answers (Mid-Level Role)

  Wipro Java Developer Interview Questions with Answers (Mid-Level Role) (Glassdoor Based – May 2024) Interview Location: Bengaluru Interview Mode: Online Candidate Role: Mid-Level Java Developer Source: Based on real experience shared on Glassdoor Review Summary: Easy and conversational. Interviewer was friendly. Focus was mainly on Java basics, internals, and real-world understanding. Q1: What is static in public static void main(String[] args) ? A: The static keyword lets the JVM call the method without creating an object. It indicates that the method belongs to the class, not instances. Q2: Why does a Java program start from the main method? A: main() is the predefined entry point of a Java application. The JVM starts execution from there. Q3: What are Checked and Unchecked Exceptions? With examples. A: Checked Exceptions : Detected at compile time. E.g., IOException , SQLException . Unchecked Exceptions : Detected at runtime. E.g., NullPointerExce...