OOP - TB1 - Tutorial

📅 2026/7/21 22:43:26
OOP - TB1 - Tutorial
TB1 Tutorial文章目录TB1 Tutorial1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.1.answer:less than 102.answer:yes3.注意前两个情况都没有break,所以程序会一直运行。answer:zerononeonezerononenone4.Write a Java program that calculates the sum of integers in the range1 to 100 (inclusive).publicclassSum{publicstaticvoidmain(String[]args){intn100;intsum0;//一定要初始化为0不能默认for(inti0;in;i){sumi;}System.out.println(sum);}}5.publicclassMutiplicationTable{publicstaticvoidmain(String[]args){intn3;intproduct0;for(inti1;in;i){for(intj1;jn;j){producti*j;System.out.print(product );}System.out.println();//记住换行的时候啥都不用写}}}6.answer:98true7.Write a block of code that calculates the sum of all the integers divisible by 3, in the range 1 to 99 (inclusive). You are not required to write a complete program.publicclassSumDivisibleBy3{publicstaticvoidmain(String[]args){intsum0;for(inti1;i100;i){if((i%3)0){sumi;}}System.out.println(sum);}}8.answer:double[]numbers{3.5,6,2.6,8.0};int[]marksnewint[60];char[]letters{a,b,c,d,e,f};String[]books{Java,SQL,PHP};9.output:Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5at Test_Q2.main(Test_Q2.java:5)Reason:ishould less than intArray, rather than10.answer:f[1] new Flower();11.answer:ArrayIndexOutOfBoundsException12.answer:variable i might not have been initializedint i0;13.answer:3636.014.publicclassStarsTriangle{publicstaticvoidmain(String[]args){introw0;intsize8;while(rowsize){intcol0;while(colrow){System.out.print(*);colcol1;}System.out.println();rowrow1;}}}15.Determine the question that should result in the following possible answersAnswer 1“This happens when a class has multiple methods with the same name but different lists of parameters.It helps ensure consistency when naming methods.”Answer 2“Methods in the same class that share the same name but accept different variable types as arguments. Such methods give programmers the flexibility to call a similar method with different types of data.”answerWhat is method overloading in Java?