SET-7
1.What is the correct syntax of for each loop?
A.for(variable:collection)
{body;}
B.for(data_type variable:collection)
{body;}
C.for(variable;collection)
{body;}
D.for(data_type variable;collection)
{body;}
2.public class temp
{
public static void main(String agrs[])
{
for(int i=1; i<=10; i++);
System.out.print(i);
}
}
A.12345678910
B.11
C.Error
D.1 2 3 4 5 6 7 8 9 10
3.public class temp
{
public static void main(String agrs[])
{
int i;
for(i=1; i<=10; i++);
System.out.print(i);
}
}
A.12345678910
B.11
C.Error
D.1 2 3 4 5 6 7 8 9 10
4.public class temp
{
public static void main(String agrs[])
{
int x[]={1,2,3,4,5};
for(int i=0; i<x.length;i++)
System.out.print(x[i]);
}
}
A.Error – length is not a method of x.
B.6
C.1,2,3,4,5
D.12345
5.public class temp
{
public static void main(String agrs[])
{
for(int i=1, int j=1; i<5 ; i++,
j++)
System.out.print(i+""+j);
}
}
A.1122334455
B.12345
C.11223344
D.Error
6.public class temp
{
public static void main(String agrs[])
{
for(int i=1, j=1; i<5 ; i++, j++)
System.out.print(i+""+j);
}
}
A.1122334455
B.12345
C.11223344
D.Error
7.for(int i=1, j=1; i<5 ; i++, j++)
System.out.print(i+j);
A.2468
B.12345
C.11223344
D.Error
8.int x = 2;
int y = 8;
while(x<(y+5))
{
System.out.println("Hello world!");
x+=2;
y-=2;
}
A.3
B.4
C.5
D.6
9.public class temp
{
public static void main(String agrs[])
{
int loop;
for(loop=1; loop<=10; loop++)
{
loop*=2;
if(loop>12)
break;
}
System.out.println(loop);
}
}
A.11
B.12
C.13
D.14
10.for(int i=10; i<=10; i++)
{
System.out.println(i)
}
A.12345678910
B.11
C.Error
D.1 2 3 4 5 6 7 8 9 10
No comments:
Post a Comment