Web Hosting Talk







View Full Version : Exams and more exams


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.

mind21_98
05-01-2003, 02:00 AM
5. Try this:

if (x != 0 && 1/x > 100)
System.out.println ("yes");

(don't know Java but the syntax seems similar to C)

Alex042
05-01-2003, 08:44 AM
It seems like many of these issues are logic related. A good logic class may help you. ;)

Maybe this will help:
6) Question 12 may help with the logic of this question and question 8 may help with the syntax.
8) You are somewhat correct. It should actually terminate BEFORE 75 as it increments with the following: 7, 18, 29, 40, 51, 62, 73 (+11 would be 84 which means it would terminate at 73)
13) As in algebra, substitute k with 4 then switch on that case number to assign x it's new value.
14) Logic should be similar to question 6 and 12.
15) This is a nested if-then statement. Substitute k with 10 then add some spaces to the beginning of some lines to help see which lines are nested.

Also, you might want to brush up on loops as it appears many of these questions deal with for-next, while, and if-thens. One error in a real program and a machine could be stuck indefinately.