1. What will the output of following JAVA program?
public class LWD{
public static void main(String [] args){
int a=0;
for(System.out.print("Learn with ");a<1;System.out.println("Durga")){
a++;
}
System.out.println("Never stop learning");
}
}
Output & Explanation:
Learn with Durga
2. What will the output of following JAVA program?
public class LWD{
public static void main(String [] args){
for(int x=0;x<20;x++);
System.out.println(x);
}
}
Output & Explanation: compile time error
In the above JAVA code, for loop end with semi colon for(int x=0;x<20;x++); denotes empty for loop for(int x=0;x<20;x++){ }; Scope of variable x, only inside for loop, next line x doesn't declare in LWD class.
class LWD{
public static void main(String [] args){
int x;
for(x=0;x<20;x++);
System.out.println(x);
}
}
3. What will be the output of following JAVA program?
class LWD{
public static void main(String l[]){
int a[]={10,20,30,40,50};
for(int i=0; i<a.length;i++)
System.out.print(a[i]+" ");
}
}
A. Error – length is not a method of a.
B. 6
C. 10,20,30,40,50
D. 10 20 30 40 50
Output & Explanation:
10 20 30 40 50
4. What is the correct syntax of for each loop?
- for(variable:collection){body;}
- for(data_type variable:collection){body;}
- for(variable;collection){body;}
- for(data_type variable;collection){body;}
Answer:
2
for(data_type variable:collection)
{body;}
5. Determine output:
public class LWD{
public static void main(String args[]){
int i, j;
for(i=1, j=0;i<10;i++) j += i;
System.out.println(i);
}
}
A. 10
B. 11
C. 9
D. 20
E. None of these
Answer: A
6. How many times will the following code print “Learn with Durga"? int count = 0;
do {
System.out.println("Learn with Durga");
count++;
} while (count < 10);
A. 8
B. 9
C. 10
D. 11
E. 0
Answer: C
7. Choose the correct statement in context of the following program code. public class LWD{
public static void main(String[] args){
double sum = 0;
for(double d = 0; d < 10;){
d += 0.1;
sum += sum + d;
}
}
}
A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d<10 would always be true.
D. The program compiles and runs fine.
Answer: D
8. What is the value of a[1] after the following code is executed? int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
A. 0
B. 1
C. 2
D. 3
E. 4
Answer: B
9. Which of the following for loops will be an infinite loop? A. for(; ;)
B. for(i=0 ; i<1; i--)
C. for(i=0; ; i++)
D. All of the above
Answer: D
10. public class While {
public void loop() {
int x= 0;
while ( 1 ) /* Line 6 */
{ System.out.print("x plus one is " + (x + 1)); /* Line 8 */ }
}
}
Which statement is true?
A. There is a syntax error on line 1.
B. There are syntax errors on lines 1 and 6.
C. There are syntax errors on lines 1, 6, and 8.
D. There is a syntax error on line 6.
Answer: D
No comments:
Post a Comment