2 Replies - 705 Views - Last Post: 26 April 2017 - 01:50 PM

#1 freesix  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 30-March 13

How to solve this questions.

Posted 19 April 2017 - 02:28 AM

Hi everybody, I come from a Job Interview for Software Developer,
One gave me a test and I'm not really though in Java, I develop in VB.Net... and I think I failed 3 of these questions.

1. First
What value of k is printed?:

	int k = 0;
	for(int i = 0; i < 28; i+= 4)
	{
		k ++;
	}	
	System.out.println(k);



I know that the response is : k=7 as I just tested it out on my PC right now, but I'd like to know how k is 7 (a clear explanation to understand). Seems like k is supposed to be "8"?

2. Second
The following loop is set to print out all the items in an array, provide a dynamic condition that will allow the for loop to still work if you were to add or remove strings from the array:

	String[] Array = {"string1", "string2", "string3"};
	for(int i =0; i < __ ; i++)
	{
		System.out.println(Array[i]);
	}



I wrote ( for(int i =0; i > 0 ; i++) ) !!!


3. Third
Solve the final value for y:

String x = "Hello";
String y = "";
if(!x.equals("Hello"))
{
	y= "Hi";
}
else
{
 	x = "How are you?";
}
if(!x.equals("Hello"))
{
	y = "Hello";
}
else 
{
	y = "Hi";
}




Here, I didn't understand very well that question, So I just skipped it.

Thanks in advance !

This post has been edited by freesix: 19 April 2017 - 02:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to solve this questions.

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13400
  • View blog
  • Posts: 53,481
  • Joined: 12-June 08

Re: How to solve this questions.

Posted 19 April 2017 - 06:58 AM

re #1 - what are the values of i? Is a 28 less than 28?

re #2 - ah, okay?

re #3 - have you followed the logic through? The string.EQUALS is pretty well worded out. Remember the ! is "not".
Was This Post Helpful? 0
  • +
  • -

#3 devitgg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 25-April 17

Re: How to solve this questions.

Posted 26 April 2017 - 01:50 PM

View Postfreesix, on 19 April 2017 - 02:28 AM, said:

Hi everybody, I come from a Job Interview for Software Developer,
One gave me a test and I'm not really though in Java, I develop in VB.Net... and I think I failed 3 of these questions.

1. First
What value of k is printed?:

	int k = 0;
	for(int i = 0; i < 28; i+= 4)
	{
		k ++;
	}	
	System.out.println(k);



I know that the response is : k=7 as I just tested it out on my PC right now, but I'd like to know how k is 7 (a clear explanation to understand). Seems like k is supposed to be "8"?

2. Second
The following loop is set to print out all the items in an array, provide a dynamic condition that will allow the for loop to still work if you were to add or remove strings from the array:

	String[] Array = {"string1", "string2", "string3"};
	for(int i =0; i < __ ; i++)
	{
		System.out.println(Array[i]);
	}



I wrote ( for(int i =0; i > 0 ; i++) ) !!!


3. Third
Solve the final value for y:

String x = "Hello";
String y = "";
if(!x.equals("Hello"))
{
	y= "Hi";
}
else
{
 	x = "How are you?";
}
if(!x.equals("Hello"))
{
	y = "Hello";
}
else 
{
	y = "Hi";
}




Here, I didn't understand very well that question, So I just skipped it.

Thanks in advance !



1. You are correct, the answer is 7. WHY? is your question, well
for(int i = 0; i < 28; i+= 4)


This sets i to 0; run while 28 or less; increase by 4.
28/4 = 7.. easy peasy.

2.) This is set by defining your array with a int variable. Example.


int y = //Whatever size the array is\\
String[] dynamicArray = new String[y];

for(x = 0; x < y; x++){
System.out.println(dynamicArray[x]);
}



3.

//commenting how you come up with solution\\
Solve the final value for y:

String x = "Hello";
String y = "";
if(!x.equals("Hello")) //if x DOES NOT equal "hello"
{
	y= "Hi"; // skip this, it does equal
}
else //do this
{
 	x = "How are you?"; //changes x value
}
if(!x.equals("Hello")) // if x DOES NOT equal "hello" -- it does now.. 
{
	y = "Hello"; // do this... the final y value;
}
else 
{
	y = "Hi";
}




the final value for y is "hello"



Hope that helped answer your confusion and hope you got the job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1