Senad
04-30-2003, 11:50 PM
Ok, for those Java people I will need some help. I'm pulling an all nighter studying for a college class I took to learn Java (Knowledge is power I guess). I'm not the sharpest knife in the drawer when it comes to Java so I will ask for help here. These are not the test questions but it is a study guide. I had a total of 30 questions and these are the only ones I am having difficulty with. No wise cracks please ;). Here are the questions:
4) What will the output be for the following code:
Public class Figment Imagination {
public static void main (String[] args)
{
String str1 = "My ideas.";
String str2 = new String();
String str3 = new String (str1);
String str4 = "My ideas";
System.out.println ("str2=" + str2);
System.out.println ("str3=" + str3);
System.out.println();
if (str1==str3)
System.out.println("str1==str3");
if (str1.equals (str3))
System.out.println ("str1.equals(str3)");
if (sr1!=str4)
System.out.println("str1!=str4");
}
}
4 Answer?) I got:
str2=
str3= My ideas.
str1.equals(str3)
str1!=str4
5) Rewrite the following line of code so that it will not crash if x==0, but it will do exactly the same thing for every other value of x:
if (1/x > 100)
System.out.println ("yes");
5 answer?) I was thinking adding an elseif statemetn saying:
elseif (x=0)
System.out.println ("yes")
6)Change the following program with a for loop to one that uses a while loop instead:
int total = 0;
for (int count = 0; count < 3; count++)
{
System.out.println(total);
total = total + count;
}
8)Which of the following loops terminate? Assuem that x has value of 7 at start of loop.
a) while (x < 75)
x+=11;
b) while (x > 0)
x+=11;
8 answer A stops after it reaches 75 while the other would go infinetly correct?
12)Below is an attempt at pseudocode to determine the maximum of a sequence of nonnegative numbers entered by the user. The user specifies the quantity of numbers first. Fine and correct any errors sin the pseudocode.
Get the quantity of numbers.
Set max to zero.
Set count of numbers processed to zero.
while (count < quantity)
get the next number.
If (number < max)
set max ot number.
increment count.
output results.
13)If k had value 4, what value will the variable x have after executing
x=2;
switch(k) {
case 3:
case 4: x=7;
case 5: x=8;
case 8: x=11;
}
14)Rewrite the following for loop using a while loop
for (int j = 2; j <12; j += 2)
y+= j*j + 7;
15)What value will the variable x have after executing
x=9;
if (k < 13)
if (k < 7)
x=5;
else
x=8;
if k has value 10?
16) Write a Java program to ocmpute the value of polynomail x^2 + 7x - 5 (x^2 means x squared). Use aSwing input dialog to input the value of x.
These are not final quesitons but look alikes. I have been trying to figure these out of the 30 questions for a while now and am having some difficulty. Thank you for your help in advance.
4) What will the output be for the following code:
Public class Figment Imagination {
public static void main (String[] args)
{
String str1 = "My ideas.";
String str2 = new String();
String str3 = new String (str1);
String str4 = "My ideas";
System.out.println ("str2=" + str2);
System.out.println ("str3=" + str3);
System.out.println();
if (str1==str3)
System.out.println("str1==str3");
if (str1.equals (str3))
System.out.println ("str1.equals(str3)");
if (sr1!=str4)
System.out.println("str1!=str4");
}
}
4 Answer?) I got:
str2=
str3= My ideas.
str1.equals(str3)
str1!=str4
5) Rewrite the following line of code so that it will not crash if x==0, but it will do exactly the same thing for every other value of x:
if (1/x > 100)
System.out.println ("yes");
5 answer?) I was thinking adding an elseif statemetn saying:
elseif (x=0)
System.out.println ("yes")
6)Change the following program with a for loop to one that uses a while loop instead:
int total = 0;
for (int count = 0; count < 3; count++)
{
System.out.println(total);
total = total + count;
}
8)Which of the following loops terminate? Assuem that x has value of 7 at start of loop.
a) while (x < 75)
x+=11;
b) while (x > 0)
x+=11;
8 answer A stops after it reaches 75 while the other would go infinetly correct?
12)Below is an attempt at pseudocode to determine the maximum of a sequence of nonnegative numbers entered by the user. The user specifies the quantity of numbers first. Fine and correct any errors sin the pseudocode.
Get the quantity of numbers.
Set max to zero.
Set count of numbers processed to zero.
while (count < quantity)
get the next number.
If (number < max)
set max ot number.
increment count.
output results.
13)If k had value 4, what value will the variable x have after executing
x=2;
switch(k) {
case 3:
case 4: x=7;
case 5: x=8;
case 8: x=11;
}
14)Rewrite the following for loop using a while loop
for (int j = 2; j <12; j += 2)
y+= j*j + 7;
15)What value will the variable x have after executing
x=9;
if (k < 13)
if (k < 7)
x=5;
else
x=8;
if k has value 10?
16) Write a Java program to ocmpute the value of polynomail x^2 + 7x - 5 (x^2 means x squared). Use aSwing input dialog to input the value of x.
These are not final quesitons but look alikes. I have been trying to figure these out of the 30 questions for a while now and am having some difficulty. Thank you for your help in advance.
