Scanner Methods Quiz

By Mickey Arnold
Last updated 10 months ago
5 Questions

What is output by the code below?
Scanner s = new Scanner("1 2 3 4 5 6");
int x = 0;
x += s.nextInt();
x += s.nextInt();
System.out.println( x );

What is output by the code below?
Scanner s = new Scanner("1 2 3 4 5 6");
int x = 0;
x += s.nextInt();
x += s.nextInt();
x += s.nextInt();
x += s.nextInt();
System.out.println( x );

What is output by the code below?
Scanner s = new Scanner("1 2 3 4 5 6");
int x = 0;
while(s.hasNext())
{
x += s.nextInt();
}
System.out.println( x );

What is output by the code below?
Scanner s = new Scanner("1 2 3 4 5 6");
int x = 0;
double z = 0;
while(s.hasNext())
{
x += s.nextInt();
z++;
}
System.out.println( x / z );

What is output by the code below?
Scanner s = new Scanner("d 1 o 2 g 3 c 7 t");
int count = 0;
while(s.hasNext())
{
if (count % 2 == 1)
System.out.print(s.next() + " ");
else
s.next();
count++;
}