Code Snippets-based MCQs

Loops

Table of contents
  1. Easy Level MCQs
  2. Slightly harder MCQs
  3. Tricky MCQs

Easy Level MCQs

The questions are based on frequently used Java 8 String methods:

  1. What will the following code print?

     String text = "Java Programming";
     System.out.println(text.substring(5));
    
    Programming
    Java Programming
    Jav
    Java
    Answer

    Programming
    (Explanation: substring(5) starts from index 5, returning the substring starting from ‘P’.)

  2. What is the return type of the method toUpperCase() in the following code?

     String str = "hello";
     String result = str.toUpperCase();
    
    String
    char
    boolean
    int
    Answer

    String
    (Explanation: toUpperCase() returns a new string where all characters are converted to uppercase.)

  3. Which method would you use to check if a string “Java” contains the substring “av”?

     String str = "Java";
    
    str.contains("av")
    str.indexOf("av")
    str.matches("av")
    str.equals("av")
    Answer

    str.contains(“av”)
    (Explanation: contains() checks if the substring exists within the string.)

  4. What does the following code output?

     String str = "Java Programming";
     System.out.println(str.replace('a', 'o'));
    
    Jovo Progromming
    Jova Progromming
    Javo Progromming
    Jova Programming
    Answer

    Jovo Progromming
    (Explanation: replace('a', 'o') replaces all occurrences of ‘a’ with ‘o’.)

  5. What is the return value of str.trim() when the string is “ Hello “?

     String str = "   Hello   ";
     System.out.println(str.trim());
    
    "Hello"
    " Hello"
    "Hello "
    " Hello "
    Answer

    “Hello”
    (Explanation: trim() removes leading and trailing whitespace from the string.)

  6. What will the following code output?

     String str = "Java";
     System.out.println(str.charAt(2));
    
    J
    a
    v
    r
    Answer

    v
    (Explanation: charAt(2) returns the character at index 2, which is ‘v’.)

  7. Which method should you use to split a string “Java Programming” into words?

     String str = "Java Programming";
    
    str.split(" ")
    str.split("")
    str.split(",")
    str.split("/")
    Answer

    str.split(“ “)
    (Explanation: split(" ") splits the string at spaces, resulting in words.)

  8. What is the return type of the toLowerCase() method?

     String str = "HELLO";
     String result = str.toLowerCase();
    
    String
    char
    boolean
    int
    Answer

    String
    (Explanation: toLowerCase() returns a new string where all characters are converted to lowercase.)

  9. What is the output of the following code?

     String str = "Java";
     System.out.println(str.indexOf('a'));
    
    0
    1
    2
    3
    Answer

    1
    (Explanation: indexOf('a') returns the index of the first occurrence of ‘a’, which is at index 1.)

  10. What does the method String.format() do?

    String result = String.format("The number is: %d", 10);
    System.out.println(result);
    
    Converts numbers to string
    Formats the string to include variables
    Replaces all occurrences of a substring
    Appends strings
    Answer

    Formats the string to include variables
    (Explanation: String.format() allows formatting strings by inserting variables like numbers into placeholders.)

  11. What will the following code print?

    String str = "  Hello World  ";
    System.out.println(str.trim().toUpperCase());
    
    HELLO WORLD
    Hello World
    HELLO WORLD
    Hello World
    Answer

    HELLO WORLD
    (Explanation: trim() removes leading and trailing spaces, and toUpperCase() converts the string to uppercase.)

  12. What will the following code output?

    String str = "Java Programming";
    System.out.println(str.contains("Pro"));
    
    true
    false
    Error
    true false
    Answer

    true
    (Explanation: contains("Pro") checks if “Pro” is a part of the string and returns true.)

  13. What does the following code do?

    String str = "Java";
    System.out.println(str.substring(1, 3));
    
    av
    Ja
    va
    J
    Answer

    av
    (Explanation: substring(1, 3) extracts the substring starting from index 1 and ending at index 3, which is “av”.)

  14. What is the result of the following expression?

    String str = "Java";
    boolean result = str.startsWith("J");
    System.out.println(result);
    
    true
    false
    Error
    true false
    Answer

    true
    (Explanation: startsWith("J") checks if the string starts with the specified character ‘J’ and returns true.)

  15. What is the return value of the following method call?

    String str = "Java";
    System.out.println(str.length());
    
    3
    4
    5
    0
    Answer

    4
    (Explanation: length() returns the number of characters in the string “Java”, which is 4.)

  16. What will the following code print?

    String str = "Hello";
    System.out.println(str.equals("hello"));
    
    true
    false
    Error
    true false
    Answer

    false
    (Explanation: equals() is case-sensitive, so “Hello” is not equal to “hello”.)

  17. What will the following code output?

    String str = "Java Programming";
    System.out.println(str.indexOf("Prog"));
    
    0
    1
    5
    -1
    Answer

    5
    (Explanation: indexOf("Prog") returns the index where the substring “Prog” begins, which is 5.)

  18. What will this code print?

    String str = "Java";
    System.out.println(str.replace('a', 'o'));
    
    Jovo
    Jova
    Java
    Jovo
    Answer

    Jovo
    (Explanation: replace('a', 'o') replaces all occurrences of ‘a’ with ‘o’, resulting in “Jovo”.)

  19. What is the return type of the method toCharArray()?

    String str = "Java";
    char[] result = str.toCharArray();
    
    String
    char[]
    char
    boolean
    Answer

    char[]
    (Explanation: toCharArray() converts the string into an array of characters and returns it.)

  20. What does the method str.equalsIgnoreCase("java") return when the string is “JAVA”?

    String str = "JAVA";
    System.out.println(str.equalsIgnoreCase("java"));
    
    true
    false
    Error
    true false
    Answer

    true
    (Explanation: equalsIgnoreCase() compares two strings ignoring case, so “JAVA” equals “java”.)

Back to Top

Slightly harder MCQs

Here are 20 more MCQs with code snippets that involve more than one method call, where the result of one method call is supplied to another:

  1. What will the following code print?

     String str = "  Java Programming  ";
     System.out.println(str.trim().toUpperCase());
    
    JAVA PROGRAMMING
    Java Programming
    JAVA programming
    JAVA PROGRAMMING
    Answer

    JAVA PROGRAMMING
    (Explanation: trim() removes leading and trailing spaces, and toUpperCase() converts the string to uppercase.)

  2. What will the following code output?

     String str = "Java Programming";
     System.out.println(str.substring(5).toLowerCase());
    
    programming
    Java programming
    JAVA PROGRAMMING
    Java programming
    Answer

    programming
    (Explanation: substring(5) extracts the substring starting at index 5, and toLowerCase() converts it to lowercase.)

  3. What is the result of the following code?

     String str = "Java Programming";
     System.out.println(str.replace("Java", "Python").toLowerCase());
    
    python programming
    Python programming
    Java programming
    python Programming
    Answer

    python programming
    (Explanation: replace("Java", "Python") replaces “Java” with “Python” and toLowerCase() converts the string to lowercase.)

  4. What will the following code print?

     String str = "    Hello World   ";
     System.out.println(str.trim().substring(0, 5));
    
    Hello
    Hello World
    Hello
    World
    Answer

    Hello
    (Explanation: trim() removes leading and trailing spaces, and substring(0, 5) extracts the first five characters.)

  5. What is the output of the following code?

     String str = "Java Programming";
     System.out.println(str.substring(0, 4).toUpperCase());
    
    JAVA
    Java
    JAV
    JAVA programming
    Answer

    JAVA
    (Explanation: substring(0, 4) extracts “Java”, and toUpperCase() converts it to “JAVA”.)

  6. What will be the output of this code?

     String str = "Hello";
     System.out.println(str.toLowerCase().replace('h', 'j'));
    
    hello
    jello
    Hello
    Jello
    Answer

    jello
    (Explanation: toLowerCase() converts “Hello” to “hello”, and replace('h', 'j') replaces ‘h’ with ‘j’.)

  7. What does the following code print?

     String str = "Java";
     System.out.println(str.concat(" Programming").toUpperCase());
    
    JAVA
    Java Programming
    JAVA PROGRAMMING
    Java Programming
    Answer

    JAVA PROGRAMMING
    (Explanation: concat(" Programming") adds “ Programming” to “Java”, and toUpperCase() converts the result to uppercase.)

  8. What will the output of the following code be?

     String str = "Java";
     System.out.println(str.substring(1).replace('a', 'o'));
    
    Jova
    Java
    Jovo
    Jaa
    Answer

    Jovo
    (Explanation: substring(1) removes the first character ‘J’, resulting in “ava”, and replace('a', 'o') changes ‘a’ to ‘o’, resulting in “Jovo”.)

  9. What does the following code print?

     String str = "  Hello World  ";
     System.out.println(str.trim().substring(0, 4).toUpperCase());
    
    HELLO
    Hello
    Hell
    WORLD
    Answer

    HELL
    (Explanation: trim() removes the spaces, substring(0, 4) extracts “Hell”, and toUpperCase() converts it to “HELL”.)

  10. What is the result of the following?

    String str = "Java Programming";
    System.out.println(str.substring(5, 15).toLowerCase().replace("programming", "rocks"));
    
    programming rocks
    programming
    rocks
    programming
    Answer

    rocks
    (Explanation: substring(5, 15) gives “Programming”, toLowerCase() converts it to “programming”, and replace("programming", "rocks") changes it to “rocks”.)

  11. What will the following code output?

    String str = "Java";
    System.out.println(str.toUpperCase().substring(1, 3));
    
    JA
    AV
    VA
    JAVA
    Answer

    VA
    (Explanation: toUpperCase() converts “Java” to “JAVA”, and substring(1, 3) extracts “VA”.)

  12. What does the following code print?

    String str = "  Hello World  ";
    System.out.println(str.trim().replace("World", "Java").toLowerCase());
    
    hello world
    hello java
    hello WORLD
    hello java world
    Answer

    hello java
    (Explanation: trim() removes leading/trailing spaces, replace("World", "Java") replaces “World” with “Java”, and toLowerCase() converts it to lowercase.)

  13. What is the result of this code?

    String str = "Java Programming";
    System.out.println(str.replace("a", "o").substring(5));
    
    ovo Progrmming
    oJava Programming
    Java Programming
    oProgrmming
    Answer

    o Progrmming
    (Explanation: replace("a", "o") changes “Java Programming” to “Jovo Progrmming”, and substring(5) removes the first 5 characters.)

  14. What will the output be of this code?

    String str = "Java Programming";
    System.out.println(str.substring(5).replace('a', 'o'));
    
    Java Progrogramming
    Jovo Progrogramming
    Progrogramming
    Progrogramming
    Answer

    Jovo Progrogramming
    (Explanation: substring(5) extracts “Programming”, and replace('a', 'o') changes ‘a’ to ‘o’.)

  15. What does the following code print?

    String str = "Hello";
    System.out.println(str.concat(" World").toUpperCase());
    
    HELLO WORLD
    hello world
    HELLO
    Hello World
    Answer

    HELLO WORLD
    (Explanation: concat(" World") appends “ World” to “Hello”, and toUpperCase() converts it to “HELLO WORLD”.)

  16. What will the following code output?

    String str = "Java";
    System.out.println(str.toLowerCase().replace('a', 'o').substring(1, 3));
    
    vo
    Oa
    va
    Jo
    Answer

    vo
    (Explanation: toLowerCase() converts “Java” to “java”, replace('a', 'o') changes ‘a’ to ‘o’, and substring(1, 3) extracts “vo”.)

  17. What will the following code print?

    String str = "Java Programming";
    System.out.println(str.substring(5, 10).toUpperCase().concat(" is fun"));
    
    PROGRAMMING is fun
    PROGR is fun
    Program is fun
    PROGRAM is fun
    Answer

    PROGRAM is fun
    (Explanation: substring(5, 10) extracts “Progr”, toUpperCase() converts it to “PROGR”, and concat(" is fun") appends “ is fun”.)

  18. What does this code print?

    String str = "  Hello World  ";
    System.out.println(str.trim().substring(6).toLowerCase());
    
    world
    hello world
    Hello world
    hello
    Answer

    world
    (Explanation: trim() removes spaces, substring(6) extracts “World”, and toLowerCase() converts it to “world”.)

  19. What will the following code print?

    String str = "Hello";
    System.out.println(str.substring(1).toUpperCase().replace('E', 'O'));
    
    Ollo
    Ollo
    HELLO
    HOLO
    Answer

    Ollo
    (Explanation: substring(1) extracts “ello”, toUpperCase() converts it to “ELLO”, and replace('E', 'O') changes ‘E’ to ‘O’, resulting in “Ollo”.)

  20. What is the result of the following code?

    String str = "Java Programming";
    System.out.println(str.substring(5, 10).toLowerCase().replace('r', 'l'));
    
    progLamming
    proGLamming
    programming
    proLamming
    Answer

    proglamming
    (Explanation: substring(5, 10) extracts “Prog”, toLowerCase() converts it to “prog”, and replace('r', 'l') changes ‘r’ to ‘l’, resulting in “proglamming”.)

Back to Top

Tricky MCQs

Here are 20 tricky and slightly confusing questions based on Java String methods:

  1. What does the following code print?

     String str = "Java";
     System.out.println(str.substring(2, 2));
    
    Empty string
    J
    a
    Error
    Answer

    Empty string
    (Explanation: substring(2, 2) extracts from index 2 to index 2, so no characters are selected, resulting in an empty string.)

  2. What is the result of the following code?

     String str = "Java Programming";
     System.out.println(str.substring(5, 20));
    
    Programming
    Error
    Java Programming
    Progr
    Answer

    Error
    (Explanation: substring(5, 20) will throw an error because the second index exceeds the length of the string.)

  3. What is the output of the following code?

     String str = "Java Programming";
     System.out.println(str.replace('a', 'o').substring(4, 10));
    
    Progra
    Progr
    Prorg
    Prora
    Answer

    Prorg
    (Explanation: replace('a', 'o') replaces ‘a’ with ‘o’, so “Java Programming” becomes “Jovo Progromming”. substring(4, 10) extracts “Prorg”.)

  4. What will the output be?

     String str = "Java";
     System.out.println(str.indexOf('a', 2));
    
    1
    2
    3
    -1
    Answer

    3
    (Explanation: indexOf('a', 2) starts searching from index 2, and the next ‘a’ is at index 3.)

  5. What is the output of this code?

     String str = "Java";
     System.out.println(str.charAt(5));
    
    Error
    a
    J
    IndexOutOfBoundsException
    Answer

    Error
    (Explanation: charAt(5) is out of bounds since the string “Java” has a length of 4.)

  6. What will the output of this code be?

     String str = "Java";
     System.out.println(str.substring(4));
    
    Empty string
    Java
    a
    Error
    Answer

    Empty string
    (Explanation: substring(4) starts at index 4, but the string has only 4 characters, so it returns an empty string.)

  7. What will the following code print?

     String str = "Java";
     System.out.println(str.replace("av", "o"));
    
    Jo
    Java
    Joo
    Javo
    Answer

    Joo
    (Explanation: replace("av", "o") replaces “av” with “o”, resulting in “Joo”.)

  8. What will the following code output?

     String str = "Java";
     System.out.println(str.indexOf("va", 1));
    
    1
    2
    3
    -1
    Answer

    2
    (Explanation: indexOf("va", 1) starts searching from index 1, and “va” is found starting at index 2.)

  9. What is the output of this code?

     String str = "Java";
     System.out.println(str.replace("a", "o").substring(1, 3));
    
    Jovo
    ovo
    Ova
    Jo
    Answer

    vo
    (Explanation: replace("a", "o") changes “Java” to “Jovo”, and substring(1, 3) extracts “vo”.)

  10. What will be the output of this code?

    String str = "Java";
    System.out.println(str.charAt(str.length() - 1));
    
    J
    a
    Error
    IndexOutOfBoundsException
    Answer

    a
    (Explanation: str.length() - 1 calculates the index of the last character, which is ‘a’.)

  11. What is the output of this code?

    String str = "Java Programming";
    System.out.println(str.substring(5, 5));
    
    Programming
    Empty string
    Java Programming
    Error
    Answer

    Empty string
    (Explanation: substring(5, 5) extracts a substring starting and ending at index 5, so the result is an empty string.)

  12. What will be the output of this code?

    String str = "Java";
    System.out.println(str.toLowerCase().concat(" Programming").substring(0, 10));
    
    java Program
    java progra
    Java Program
    java program
    Answer

    java program
    (Explanation: toLowerCase() converts “Java” to “java”, concat(" Programming") appends “ Programming”, and substring(0, 10) extracts “java progra”.)

  13. What does this code output?

    String str = "Hello World";
    System.out.println(str.indexOf('o', 5));
    
    5
    7
    4
    6
    Answer

    7
    (Explanation: indexOf('o', 5) searches for ‘o’ starting from index 5, and the next ‘o’ is at index 7.)

  14. What will this code print?

    String str = "Java Programming";
    System.out.println(str.replace(" ", ""));
    
    Java Programming
    JavaProgramming
    Error
    Java
    Answer

    JavaProgramming
    (Explanation: replace(" ", "") removes all spaces from the string.)

  15. What is the output of this code?

    String str = "Java Programming";
    System.out.println(str.replace("a", "o").substring(5, 15).toUpperCase());
    
    OPROGRAMMING
    PROGR OGRAMMING
    PROGR OGRAMMING
    PROGRAMMING
    Answer

    OPROGRAMMING
    (Explanation: replace("a", "o") replaces “a” with “o”, then substring(5, 15) extracts “Programming”, and toUpperCase() converts it to uppercase.)

  16. What is the result of the following?

    String str = "Hello";
    System.out.println(str.substring(1, 3).toUpperCase());
    
    EL
    He
    lo
    HEL
    Answer

    EL
    (Explanation: substring(1, 3) extracts “el”, and toUpperCase() converts it to “EL”.)

  17. What will this code print?

    String str = "Java Programming";
    System.out.println(str.indexOf("Java") == 0);
    
    true
    false
    Error
    false
    Answer

    true
    (Explanation: indexOf("Java") == 0 checks if “Java” starts at index 0, which is true.)

  18. What does this code print?

    String str = "Java";
    System.out.println(str.charAt(str.length()));
    
    a
    Error
    Java
    IndexOutOfBoundsException
    Answer

    Error
    (Explanation: charAt(str.length()) causes an error because index str.length() is out of bounds.)

  19. What will the following code print?

    String str = "   Hello ";
    System.out.println(str.trim().length());
    
    5
    6
    7
    4
    Answer

    5
    (Explanation: trim() removes the spaces, leaving “Hello”, and the length is 5.)

  20. What does this code print?

    String str = "Java Programming";
    System.out.println(str.substring(5, 20));
    
    Error
    Progr
    Programming
    Program
    Answer

    Error
    (Explanation: The second argument in substring(5, 20) is greater than the length of the string, so it throws an error.)

Back to Top