In: Computer Science
1. The "else" clause is required for a decision statement. 2. A String variable can be assigned to an integer variable. 3. An integer variable can be assigned to a boolean variable. 4. We can calculate the square root of numbers with the class java.util.Math. 5. String is a Java primitive data type. 6. A ‘Double’ object can be assigned to a ‘String’ data variable. 7. Every Java class has to have a static main() method to avoid compilation error. 8. The Random class has a method that can generate binary random numbers. 9. There is no compilation error if we assign a boolean variable to another integer variable. 10. It’s fine to use “new Scanner()” without importing any package. 11. A switch statement must have the “default” clause 12. When you increment the value of an un-initialized integer variable, the variable value will be 1 larger than before 13. We can use String equals() method to determine if a string is larger than another string. 14. JOptionPane.showInputDialog() does not return an integer value. 15. To declare a constant integer, we can use ‘const int’. 16. We can use the Integer class to convert a string to an integer. 17. The "%d" formatter can be used in System.out.printf() to format a string 18. It’s possible to compare a string and an integer. 19. An “if” statement can be nested inside a “switch” statement.
Write true or false for this objective questions in java language
1. False. "If" need not have an "else" statement.
2. False. No, these are two incompatible types and string cannot be assigned to integer.
3. False. Integer cannot be assigned to boolean.
4. False. sqrt is in "java.lang.Math" not "java.util.Math".
5. False. "String" is not a primitive type, only byte, short, int, long, float, double, boolean and char.
6. False. Double cannot be assigned to String.
7. False. Only the class which is executed at the start must have a main method, other classes need not.
8. True. The "nextBoolean" function in Random class can generate binary random numbers.
9. False. Boolean cannot be assigned to integer.
10. False. "Scanner" is in java.util which needs to be imported to use Scanner.
11. False. Default is not necessary in switch.
12. False. Java gives an error if an operation is performed on an unitialized variable.
13. False. "equals()" only checks if two strings are the same. It cannot be used for comparison.
14. True. showInputDialog() returns a String, not an integer.
15. False. "const" is not a Java keyword, "final" must be used instead.
16. True. The "parseInt" method in Integer class can be used to convert string into an integer.
17. False. "%d" is used to format integer values, not string.
18. False. Integer and string cannot be compared.
19. True. Yes, an "if" can be nested inside "switch".
Comment in case of any doubts.