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

TCS Java Interview | Java | Spring Boot | Microservices | Database

TCS Java Interview | Java | Spring Boot | Microservices | Database This post is based on a Java backend developer interview simulation and provides direct transcript-based answers to help you prepare for similar interviews. 📺 This interview scenario was inspired by a video from the  YouTube channel:  CloudTech . 🔗 Watch the full video here:  https://www.youtube.com/watch?v=yVj2EgwZxk4 1. Can you introduce yourself and talk about your tech stack and domains? I have around 4.5 years of experience working as a Java developer. My core expertise is in Java and developing REST APIs using Spring Boot. I also have basic knowledge of microservices architecture. On the database side, I work with SQL and Oracle. Additionally, I have exposure to UI technologies like Angular, HTML, and CSS, but they are secondary skills. My primary focus is backend Java development. 2. Which version of Java are you currently working on? We are currently using Java 8 but are in the process of upgr...

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers)

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers) * * * 1. What is Microservices Architecture?   → Microservices architecture breaks an application into small, independent services. Each one handles a specific business function and can be developed, deployed, and scaled independently. They usually communicate over REST APIs or messaging queues. * * * 2. How does Spring Boot help in building microservices?   → Spring Boot offers embedded servers, auto-configuration, starter templates, and production-ready tools. It works well with Spring Cloud to provide service discovery, config management, load balancing, and more. * * * 3. What are the core components in a microservices setup?   → Key components include:   ✓ API Gateway   ✓ Eureka (Service Discovery)   ✓ Config Server   ✓ Kafka or RabbitMQ   ✓ Circuit Breaker (like Resilience4j)   ✓ Centralized logging and monitoring (ELK, Prometheus) * * * 4. What i...

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...