Hi,
I don't know how to read the below code to produce the outputs.
QUESTION:State the expected output from the below codes. [5]
for(int x=5; x <=100; x++)
{
if(x % 10) ==0)
break;
System.out.println(x);
}
QUESTION: Trace the following code, and produce its outputs. [5]
String data = ‘’data base’’;
int x = data.length();
int y = data.indexOf(‘a’);
int z = data.lastIndexOf(‘g’);
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(10%x);
System.out.println(data.equals(‘’database’’));
QUESTION:State the expected output from the below codes. [5]
for(int x=5; x {
if(x % 10) ==0)
break;
System.out.println(x);
}
ANSWER :
its a program that terminate when your if statement is test(if its true it then terminate ) i.e if the result is equal to zero it then terminate.
for the program above your first input value is x=5 and (5%10==0) is not equal to zero so your first output is 5.
your next x=6 because X=X+1 base on your increment (6%10==0) is not equal to zero so your next output is 6
continuously you keep incrementing x by 1 i.e X= X+1
up to x=10 and (10%10==0) is true so the program then terminate ....
therefore your outputs = [5,6,7,8,9]
QUESTION: Trace the following code, and produce its outputs. [5]
String data = ‘’data base’’;
int x = data.length();
int y = data.indexOf(‘a’);
int z = data.lastIndexOf(‘g’);
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(10%x);
System.out.println(data.equals(‘’database’’));
ANSWER :
x will be 9
y will be 1
z will be -1
10%x will be 1
data.equals(‘’database’’) will be false
Pages
Add new comment