Fresher Interview Looping Program with Answer

In one of the Fresher Interviews, our trainees were asked to write a program on looping. Here with we are going to see what is the program and how are we going to derive the answer.

What is the Fresher Interview Looping Program?

The Interview program asked is very simple. We need to get a number from user. Based on the number given, we need to print {}. For example, If the user says 2, two opening braces and two closing braces should be printed as {} {{}}. If the number is 3, output should be {} {{}} {{{}}}}.

It is very normal to ask the Freshers to write any logical program during interview nowadays. Though they have to answer few Conceptual questions, writing programs in Interview is inevitable nowadays.

Shall we move on to Fresher Interview Looping Program with Answer:

Yes, sure. We are requested to print two letters, opening curly brace and closing curly brace. To achieve this,

1) lets have two variables named a and b.

char a = ‘{’, b = ‘}’;

2) Now, get the count from user. Lets consider the value as 3.

int count = 3; 

3) Its time to print opening curly brace now. We shall go with a for loop for this.

for(int i=1; i<=count; i++)
	System.out.print(a+ “ ”); 

4) After printing opening curly brace, lets print closing curly brace.

for(int i=1; i<=count; i++)
System.out.print(b+ “ ”);

Thus we shall get if 3 is given as input, three times {} will be printed. Let us consolidate all code here.

char a = ‘{’, b = ‘}’; 
int count = 3; 
for(int i=1; i<=count; i++)
	System.out.print(a+ “ ”); 
for(int i=1; i<=count; i++)
	System.out.print(b+ “ ”); 

for count value = 1: 
char a = ‘{’, b = ‘}’; 
int count = 1; 
for(int i=1; i<=count; i++)
	System.out.print(a+ “ ”); 
for(int i=1; i<=count; i++)
	System.out.print(b+ “ ”); 

for count value = 2: 
char a = ‘{’, b = ‘}’; 
int count = 2; 
for(int i=1; i<=count; i++)
	System.out.print(a+ “ ”); 
for(int i=1; i<=count; i++)
	System.out.print(b+ “ ”); 


If you observe keenly, the only difference between all three loops is, the count value. Hence, let us create one outer loop to get correct output.

for (int count = 1; count<=3; count++)
{ 
char a = '{', b = '}';  
for(int i=1; i<=count; i++)
	System.out.print(a); 
for(int i=1; i<=count; i++)
	System.out.print(b); 
System.out.println(); 
}

Final Output:

Fresher Interview Looping Program with Answer
Fresher Interview Program wth Answer

If you wish to have a video tutorial of this post, you can check it here.