-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Quiz & Code Challenges #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
178 commits
Select commit
Hold shift + click to select a range
bac5daf
update
amideb 4638517
update
amideb 9648ec1
update
amideb 6f26323
update
amideb 26f7ea2
update
amideb 62de71f
update
amideb 2516d30
update
amideb 1dda794
update
amideb a1593b9
update
amideb 151c0e0
update
amideb 61b6aae
update
amideb 97bc05c
update
amideb 04e0d05
update
amideb 1f48644
update
amideb f8193a7
update
amideb fee7d79
Thank You
amideb 1020d09
Thank You
amideb e542acc
Thank You
amideb 19dc1f5
.
amideb 341886f
.
amideb d15b969
.
amideb 40e9640
.
amideb a8e86d7
.
amideb 34cc46e
.
amideb 1c73b37
.
amideb 83217ba
.
amideb d195173
.
amideb d6e97bc
.
amideb ff1eb88
.
amideb cc408a3
.
amideb 31436f5
.
amideb 006d202
.
amideb cfb97dc
Update CODECHALLENGE.md
amideb 649d7ca
Update QUIZ.md
amideb ad9fee4
.
amideb e98c08e
.
amideb 08f0de9
.
amideb cb06073
.
amideb ff5d09e
.
amideb 665b4c4
.
amideb 3ee9713
.
amideb 984221b
.
amideb de4716c
.
amideb f22d03a
.
amideb 44d9a6a
.
amideb da24e64
.
amideb 3d94e7f
.
amideb d4c096f
.
amideb 06cfcd7
.
amideb c3e87eb
.
amideb d388148
.
amideb d16dc28
.
amideb 8c8553a
.
amideb 720ec45
.
amideb 5374506
.
amideb 60ccc32
.
amideb 9c96c61
.
amideb 851991c
.
amideb 92ad6d4
.
amideb 9c24144
.
amideb cae00fa
.
amideb a84c144
.
amideb 05862d7
.
amideb dcca7ee
.
amideb c9b8fa3
.
amideb 3cf98f3
.
amideb 59e35d8
.
amideb 0932244
.
amideb 828f6b6
.
amideb 8ff106b
.
amideb 3d2464f
.
amideb 6ac4fc8
.
amideb faaf7a5
.
amideb 8fa517d
.
amideb 27f81fb
.
amideb 586f618
.
amideb 35035be
.
amideb 942c26d
.
amideb 7f188b8
.
amideb 58cbb7f
.
amideb 8d1d399
.
amideb 7aa5189
.
amideb 4b62fb6
.
amideb d149c3a
.
amideb b4cd7cd
.
amideb fe2723b
.
amideb 81df5f5
.
amideb ba759da
.
amideb 82440b0
.
amideb 780e781
Create 03-IntroductionToJavaPlatform.md
amideb 2a1b5be
.
amideb ef0533f
.
amideb 28854b3
.
amideb f7c1d9e
.
amideb 944c1fc
.
amideb f4da97f
.
amideb d221fc4
Update 05-IntroductionToObjectOrientedProgrammin.md
amideb 050714b
.
amideb f6b8f3f
.
amideb 9e99b5b
.
amideb 655ede0
.
amideb f2b9c19
.
amideb a61fefb
Update Q05-IntroductionToObjectOrientedProgrammin.md
amideb 2a7cc0b
Update Q05-IntroductionToObjectOrientedProgrammin.md
amideb aaef942
.
amideb cacd7ce
.
amideb 1bfa155
Update 03-IntroductionToJavaPlatform.md
amideb 5288e77
.
amideb d3ab262
.
amideb a781dc0
.
amideb dca7682
.
amideb e0f6f38
.
amideb dc9998e
.
amideb 2441d3a
Update 05-RENEW.md
amideb 0028f48
...
amideb 825d9e4
.
amideb bc5fb0d
.
amideb b6f2245
.
amideb 141a254
.
amideb 54920d0
.
amideb 374b3b3
.
amideb ac414cb
.
amideb f638928
.
amideb 407ade5
Update 06-PrimitiveDataTypesAndAlternatives.md
amideb 31ccc91
.
amideb e6a3851
.
amideb 4815fb1
Update 07-Conditionals.md
amideb 45ce4be
h
amideb 636eaca
.
amideb 4f1b5a6
.
amideb 7f41611
.
amideb 913d49a
.
amideb 11adc12
Update 08-Loops.md
amideb ed45d45
.
amideb aed13df
.
amideb 092427b
.
amideb 5efc571
.
amideb 2544a1a
.
amideb 91d03e9
.
amideb ab0ff2b
.
amideb f93f296
.
amideb 48c340b
.
amideb 6c8e3db
.
amideb 117eb74
.
amideb f728364
.
amideb 5034a90
.
amideb 8ffaebc
.
amideb bd17633
.
amideb 8805d42
.
amideb bce3b6f
.
amideb 1da13d3
.
amideb 981c59e
Update Q11-ObjectOrientedProgrammingAgain.md
amideb 9e5c910
.
amideb 987017f
.
amideb 8b90951
.
amideb cec806b
.
amideb 93abae0
.
amideb 6624f60
.
amideb a65c107
.
amideb a42530a
Update Q02-IntroductionToMethods-MultiplicationTable.md
amideb f5556db
.
amideb 51ff016
.
amideb cac877b
.
amideb 3d3dbbe
Create 1.md
amideb c2b3a7c
Rename 1.md to Q01-IntroductionToJavaProgrammingWithJShell-Multiplica...
amideb 598db48
Create Q13-Generics.md
amideb 4daf212
Create Q13-Generics.md
amideb 620ffe9
Create Q14-FunctionalProgramming.md
amideb 45d32eb
Create Q15-ThreadsAndConcurrency.md
amideb 5abd8a3
Create Q16-ExceptionHandling.md
amideb 0dbc4da
Create Q17-Files.md
amideb 1390869
Create Q17-Files.md
amideb 4529da9
Create Q16-ExceptionHandling.md
amideb 80e60f6
Create Q15-ThreadsAndConcurrency.md
amideb 908ad54
Update Q15-ThreadsAndConcurrency.md
amideb 4644ee2
Create Q14-FunctionalProgramming.md
amideb 85b2180
Update Q14-FunctionalProgramming.md
amideb a32cc91
.
amideb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
664 changes: 664 additions & 0 deletions
...-Explanation/Q01-IntroductionToJavaProgrammingWithJShell-MultiplicationTable.md
Oops, something went wrong.
251 changes: 251 additions & 0 deletions
00-Quiz-Explanation/Q02-IntroductionToMethods-MultiplicationTable.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
### Question 1 | ||
|
||
|
||
|
||
What does the `void` keyword indicate in a method definition? | ||
|
||
|
||
|
||
* A) The method returns an integer value. `Incorrect: void` means no return type, not integer. | ||
|
||
* B) The method does not return any value. `Correct: void` means the method does not return any value. | ||
|
||
* C) The method returns a boolean value. `Incorrect: void` means no return type, not boolean【5†source】. | ||
|
||
|
||
|
||
### Question 2 | ||
|
||
|
||
|
||
What is the correct way to call a method named `sayHelloWorldTwice`? | ||
|
||
|
||
|
||
* A) sayHelloWorldTwice. `Incorrect: Method calls in Java require parentheses, even if no arguments are being passed`. | ||
|
||
* B) sayHelloWorldTwice(). `Correct: This is the correct syntax to call a method in Java`. | ||
|
||
* C) sayHelloWorldTwice(); `Incorrect: While this is valid in a statement context, it's not the correct way to just refer to a method call`【6†source】. | ||
|
||
|
||
|
||
### Question 3 | ||
|
||
|
||
|
||
Which of the following statements is true about method definitions and method calls? | ||
|
||
|
||
|
||
* A) Defining a method automatically executes its statement body. `Incorrect: Defining a method does not execute its body`. | ||
|
||
* B) Defining a method and invoking it are the same thing. `Incorrect: Defining a method is different from invoking it`. | ||
|
||
* C) Defining and invoking methods are two different steps. `Correct: In Java, a method must first be defined and then invoked for the code in the method to execute`【7†source】. | ||
|
||
|
||
|
||
### Question 4 | ||
|
||
|
||
|
||
What is the correct syntax for defining a method that prints "Hello World"? | ||
|
||
|
||
|
||
* A) void printHelloWorld() { System.out.println("Hello World"); }. `Correct: This is the correct syntax for defining a method in Java`. | ||
|
||
* B) printHelloWorld() { System.out.println("Hello World"); }. `Incorrect: Java methods require a return type, in this case, 'void' is missing`. | ||
|
||
* C) void printHelloWorld { System.out.println("Hello World"); }. `Incorrect: Parentheses are required after the method name to indicate it's a method`【8†source】. | ||
|
||
|
||
|
||
### Question 5 | ||
|
||
|
||
|
||
Which of these method names follows the same naming rules as variable names? | ||
|
||
|
||
|
||
* A) 1stMethod. `Incorrect: Variable names cannot start with a number`. | ||
|
||
* B) method_one. `Correct: This follows the Java naming rules, can contain alphanumeric characters and underscores, and cannot start with a number`. | ||
|
||
* C) first-Method. `Incorrect: Hyphens are not allowed in Java method names`【9†source】. | ||
|
||
|
||
|
||
### Question 6 | ||
|
||
|
||
|
||
Which command lists the methods defined in the current JShell session? | ||
|
||
|
||
|
||
* A) /methods. `Correct: This command lists the methods defined in the current JShell session`. | ||
|
||
* B) /list. `Incorrect: This command lists the statements entered during the current JShell session`. | ||
|
||
* C) /edit. `Incorrect: This command opens a method definition in a separate editor window`【10†source】. | ||
|
||
|
||
|
||
### Question 7 | ||
|
||
|
||
|
||
What does the /edit command do in JShell? | ||
|
||
|
||
|
||
* A) Lists the code of the specified method. `Incorrect: This is not the function of the /edit command`. | ||
|
||
* B) Allows you to modify the method definition in a separate editor window. `Correct: The /edit command opens the definition of a method in a separate editor window for modification`. | ||
|
||
* C) Saves the session method definitions to a file. ``Incorrect: This is the functionality of the /save command, not /edit`【11†source】. | ||
|
||
|
||
|
||
### Question 8 | ||
|
||
|
||
|
||
What is the correct syntax for defining a method with an argument? | ||
|
||
|
||
|
||
* A) void methodName(ArgType argName) { method-body }. `Correct: This is the correct syntax to define a method with arguments in Java`. | ||
|
||
* B) methodName(ArgType argName) { method-body }. `Incorrect: Java methods require a return type`. | ||
|
||
* C) methodName(ArgType argName) { method-body; }. `Incorrect: The semicolon at the end is not necessary and the return type is missing`【12†source】. | ||
|
||
|
||
|
||
### Question 9 | ||
|
||
|
||
|
||
Which method definition correctly prints all integers from 1 to n (inclusive), where n is an argument? | ||
|
||
|
||
|
||
* A) | ||
|
||
|
||
|
||
void printNumbers(int n) { | ||
|
||
for (int i = 1; i <= n; i++) { | ||
|
||
System.out.println(i); | ||
|
||
} | ||
|
||
} | ||
|
||
`Correct: This method correctly prints all integers from 1 to n`【13†source】. | ||
|
||
|
||
|
||
### Question 10 | ||
|
||
|
||
|
||
What would happen if you try to call the method `sayHelloWorld` with a string argument, when the method is defined to accept an integer argument? | ||
|
||
|
||
|
||
* A) The program will compile and run without any errors. `Incorrect: The method call will fail to compile due to incompatible types`. | ||
|
||
* B) The program will throw a runtime error. `Incorrect: The error will occur at compile time, not runtime`. | ||
|
||
* C) The program will fail to compile due to incompatible types. `Correct: Java is statically-typed, meaning the type of each variable and expression is checked at compile time`【14†source】. | ||
|
||
|
||
|
||
|
||
### Question 11 | ||
|
||
What is method overloading in Java? | ||
|
||
* A) The ability to have multiple methods with the same name in a class, but with different types of arguments. `Correct: Method overloading allows multiple methods with the same name but different parameter lists in a class`. | ||
* B) The ability to have multiple methods with the same name and the same types of arguments in a class. `Incorrect: Methods with the same name and argument types are considered duplicates and will result in a compilation error`. | ||
* C) The ability to have a single method with an arbitrary number of arguments. `Incorrect: This describes varargs, not method overloading`【16†source】. | ||
|
||
### Question 12 | ||
|
||
Consider the following two method definitions: | ||
|
||
```java | ||
void printName(String firstName, String lastName) { | ||
System.out.println(firstName + " " + lastName); | ||
} | ||
|
||
void printName(String firstName, String middleName, String lastName) { | ||
System.out.println(firstName + " " + middleName + " " + lastName); | ||
} | ||
``` | ||
|
||
Which of the following statements are true? | ||
|
||
- A) The two methods are overloaded methods. `Correct: These methods have the same name but different parameters, which is a feature of method overloading`. | ||
- B) The two methods have the same name and different number of arguments. `Correct: These methods indeed have the same name but different number of arguments`. | ||
- C) The two methods have the same name and the same types of arguments. `Incorrect: While they have the same types of arguments, the number of arguments is different, so this statement is false`【17†source】. | ||
|
||
### Question 13 | ||
|
||
What will the following code snippet output? | ||
|
||
``` | ||
void sum(int a, int b) { | ||
System.out.println(a + b); | ||
} | ||
|
||
void sum(int a, int b, int c) { | ||
System.out.println(a + b + c); | ||
} | ||
|
||
sum(1, 2); | ||
sum(1, 2, 3); | ||
``` | ||
|
||
- A) The output will be 3 6. `Correct: The first call to sum uses the two-parameter version, while the second call uses the three-parameter version`. | ||
- B) The output will be 3 5. `Incorrect: The second call to sum(1,2,3) would yield 6, not 5`. | ||
- C) The code will not compile due to method overloading. `Incorrect: Method overloading is a valid concept in Java, and this code would compile and run correctly`【18†source】. | ||
|
||
### Question 14 | ||
|
||
Which of the following statements is true about methods with multiple arguments in Java? | ||
|
||
- A) A method can only accept up to 2 arguments. `Incorrect: A method in Java can accept any number of arguments`. | ||
- B) A method can accept any number of arguments, but they must be of the same type. `Incorrect: A method can accept arguments of different types`. | ||
- C) A method can accept any number of arguments, and they can be of different types. `Correct: In Java, a method can accept any number of arguments, and these arguments can be of different types`【19†source】. | ||
|
||
### Question 15 | ||
|
||
What is the main purpose of method overloading in Java? | ||
|
||
- A) To reduce code duplication by allowing methods with the same name but different arguments. `Correct: One of the benefits of method overloading is that it allows the same method name to be used with different parameters`. | ||
- B) To allow a method to return different types of values based on the input arguments. `Incorrect: Method overloading does not affect the return type of a method`. | ||
- C) To create multiple methods with the same name and the same number of arguments, but with different implementation. `Incorrect: The methods would need to have different parameter lists to be considered overloaded`【20†source】. | ||
|
||
### Question 16 | ||
|
||
What is the purpose of a return statement in a method? | ||
|
||
- A) To end the execution of the method. `Incorrect: While a return statement does end the execution of a method, its main purpose is to return the result of a computation to the calling code`. | ||
- B) To return the result of a computation to the calling code. `Correct: The primary purpose of the return statement in a method is to return the result of a computation to the calling code`. | ||
- C) To print the output of the method. `Incorrect: Printing the output is the job of the System.out.println() method, not the return statement`【21†source】. | ||
|
||
### Question 17 | ||
|
||
What is the benefit of using a return mechanism in a method? | ||
|
||
- A) It allows the method to print the result of the computation. `Incorrect: Printing is not the job of the return statement, rather it returns the result to the calling code`. | ||
- B) It enables sharing computed results with other code and methods, and improves breaking down a problem into sub-problems. `Correct: The return mechanism enables the reuse of computed results in other parts of the code`. | ||
- C) It simplifies the syntax of the method. `Incorrect: While the use of return can make a method more readable, it does not necessarily simplify the syntax`【22†source】. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.