先决条件: Java中的决策 预测以下程序的输出
1.以下程序的输出是什么?
public class Test { public static void main(String[] args) { int x = 10 ; if (x) { System.out.println( "HELLO GEEKS" ); } else { System.out.println( "BYE" ); } } } |
选项: 1.你好,极客们 2.编译时错误 3.运行时错误 4.再见 输出:
The answer is option (2).
说明: if语句的参数应为布尔类型。错误地,如果我们试图提供任何其他数据类型,那么我们将得到编译时错误,表示不兼容的类型。这里的参数是int类型的,因此我们将得到编译时错误,错误是error:compatible types: int不能转换为Boolean
2.以下程序的输出是什么?
public class Test { public static void main(String[] args) { int x = 10 ; if (x) System.out.println( "HELLO GEEKS" ); System.out.println( "WELCOME" ); else { System.out.println( "BYE" ); } } } |
选项: 1.你好,极客们 欢迎 2.你好,极客们 3.再见 4.编译时错误 输出:
The answer is option (4)
说明: 在if零件中,大括号是可选的。如果没有大括号,if下只允许一条语句。如果我们试图给出多个语句,那么我们将得到编译时错误,错误是error:’else’而不是’If’。
3.以下程序的输出是什么?
public class Test { public static void main(String[] args) { int x = 10 , y = 20 ; if (x < y) int a = 10 ; else { System.out.println( "BYE" ); } } } |
选项: 一点一零 2.再见 3.无输出 4.编译时错误 输出:
The answer is option (4).
说明: 在上面的程序中,if语句中有一个声明性语句,没有大括号,这就是为什么我们会得到编译时错误,说error:variable declaration not allowed here。
4.以下程序的输出是什么?
public class Test { public static void main(String[] args) { int x = 10 , y = 20 ; if (x < y) { if (x > y) { System.out.println( "HELLO GEEKS" ); } else { System.out.println( "WELCOME" ); } } } } |
输出: 1.你好,极客们 2.编译时错误 3.欢迎 4.无输出
The answer is option (3)
说明: java中没有其他悬而未决的问题。每个else都映射到最近的if语句。这里,内部else if映射为最近的if部分,即if(x>y)。
5.以下程序的输出是什么?
public class Test { public static void main(String[] args) { if ( true ) ; } } |
选项: 1.无输出 2.编译时错误 3.运行时错误 4.运行时异常 输出:
The answer is option (1)
说明: ;(分号)是有效的java语句,也称为空语句。因此,我们也可以将其应用于if语句中。
6.以下程序的输出是什么?
class Test { public static void main(String[] args) { String day = "Sunday" ; switch (day) { case "Monday" : System.out.println( "Let's Work" ); break ; case "Saturday" : System.out.println( "waiting for Sunday" ); break ; case "Sunday" : System.out.println( "Today is fun day" ); } } } |
选项: 1.编译时错误 2.让我们一起工作 3.运行时错误 4.今天是有趣的一天 输出:
The answer is option (4)
说明: 允许的参数类型 switch语句 在1.4版本之前是字节、短字符、字符和整数。但从1.5版开始,相应的包装类和枚举类型也被允许。从1.7版开始,还允许使用字符串类型。参考 这 文章详细介绍
7.以下程序的输出是什么?
public class MainClass { enum day { MON, SAT, SUN } public static void main(String[] args) { day ch = day.SUN; switch (ch) { case MON: System.out.println( "Lets work!" ); break ; case SAT: System.out.println( "Waiting for sunday" ); break ; case SUN: System.out.println( "Lets have fun!" ); break ; } } } |
选项: 1.无输出 2.语法错误 3.让我们玩得开心! 4.编译时错误 输出:
The answer is option (3)
说明: switch语句允许的参数类型为byte、short、char、int,直到1.4版本。但从1.5版开始,相应的包装类和枚举类型也被允许。从1.7版开始,还允许使用字符串类型。
8.以下程序的输出是什么?
class MainClass { public static void main(String[] args) { int x = 10 ; Switch(x) { System.out.println( "GEEKS" ); } } } |
选项: 1.极客 2.编译时错误 3.无输出 4.运行时错误 输出:
The answer is option (2)
说明: 在switch内部,每个语句都应该在某种情况下或默认情况下,即在switch内部不允许独立,否则我们将得到编译时错误,错误是:“;”预期。
9.以下程序的输出是什么?
class MainClass { public static void main(String[] args) { int x = 10 ; int y = 20 ; switch (x) { case 10 : System.out.println( "HELLO" ); break ; case y: System.out.println( "GEEKS" ); break ; } } } |
选项: 1.你好 2.无输出 3.极客 4.编译时错误 输出:
The answer is option (4)
说明: 每个case标签都应该是常量,否则会出现编译时错误。但我们可以添加变量作为case标签,但我们必须将该变量声明为final。但在这里,我们使用变量y作为case标签,这就是为什么我们会得到编译时错误,称为error:constant expression required。
10.以下程序的输出是什么?
class MainClass { public static void main(String[] args) { int x = 10 ; final int y = 20 ; switch (x) { case 10 : System.out.println( "HELLO" ); break ; case y: System.out.println( "GEEKS" ); break ; } } } |
选项: 1.极客 2.编译时错误 3.你好 4.无输出 输出:
The answer is option (3)
说明: 每个case标签都应该是常量,否则会出现编译时错误。但我们可以添加变量作为case标签,但我们必须将该变量声明为final。但在这里,我们使用变量y作为案例标签,这就是为什么我们会得到结果HELLO。
11.以下程序的输出是什么?
class MainClass { public static void main(String[] args) { int x = 10 ; switch (x + 1 + 1 ) { case 10 : System.out.println( "HELLO" ); break ; case 10 + 1 + 1 : System.out.println( "GEEKS" ); break ; } } } |
输出: 1.编译时错误 2.极客 3.你好 4.无输出
The answer is option (2).
说明: 开关参数和大小写标签都可以是表达式。但case标签应该是常量表达式。在这里,案例标签“10+1+1”被视为案例12,开关参数“x+1+1”也被视为案例12。
12.以下程序的输出是什么?
class MainClass { public static void main(String arg[]) { char stream = 'C' ; int x = 2 ; switch (x) { case 1 : System.out.println( "SCIENCE, MATHS, PHYSICS" ); break ; case 2 : switch (stream) { case 'A' : System.out.println( "Welcome" ); break ; case 'C' : System.out.println( "Geeksforgeeks" ); break ; case 'B' : System.out.println( "Have a nice day" ); break ; } break ; case 3 : switch (stream) { case 'C' : System.out.println( "Welcome" ); break ; case 'D' : System.out.println( "In" ); break ; case 'E' : System.out.println( "GFG" ); break ; } break ; } } } |
选项: 1.编译时错误 2.GFG 3.无输出 4.极客 输出:
The answer is option (4)
说明: 嵌套开关大小写在java中是可能的。嵌套switch语句是另一个switch语句中的switch语句。
本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。