LPU Java Unit 2 Lecture 9 MCQ Loops RB | LPUCOLAB

LPU Java Unit 2 Lecture 9 MCQ Loops RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 9_MCQ_Loops_RB

1. What is the result of the following code?

class Main {
    public static void main(String[] args) {
        do {
            int count = 0;    
            do {
                count++;
            } while (count < 2);
            System.out.println(count);
            break;
        } while (true);
    }
}

Answer:
2


2. What is the result of the following code?

public class Main {
    public static void main(String[] args) {
        for(char c = 'a' ; c < 'd'; c++){
            System.out.print(c);
        }
    }
}

Answer:
abc


3. What is the result of the following code?

public class Main {
    public static void main(String[] args) {
        for (char ch : "12345".toCharArray()) {
            if (ch == '3') {
                continue;
            }
            System.out.print(ch + " ");
        }
    }
}

Answer:
1 2 4 5


4. What is the result of the following code?

public class Main {
    public static void main(String[] args) {
    int sum = 0;
    for (char ch : "12345".toCharArray()) {
        sum += ch - '0';
    }
    System.out.println(sum);
    }
}

Answer:
15


5. What will be the output of this program?

public class Main {
    public static void main(String[] args) {
        for ( int j = 0, k = 5; j < k; k--) ;
        for ( int j = 0; j++ < 3;) ;    
        for ( int i = 0; i < 5; i++, System.out.print(i + ".Hi ")) ;
    }   
}

Answer:
1.Hi 2.Hi 3.Hi 4.Hi 5.Hi