slug
type
status
category
summary
date
tags
password
icon
Key Points and Sample Codes
1.1 Why Programming? Why Java?
- Key Points: Programming enables the solution of complex problems and automates repetitive tasks. Java is widely used due to its portability, object-oriented features, and extensive API.
- Sample Code:
1.2 Variables and Data Types
- Key Points: Variables store data values. Java supports several data types such as int, double, char, and boolean.
- Sample Code:
1.3 Expressions and Assignment Statements
- Key Points: Expressions are used to compute values. Assignment statements assign the result to a variable.
- Sample Code:
1.4 Compound Assignment Operators
- Key Points: Compound operators perform an operation and assignment in one step (e.g., +=, -=).
- Sample Code:
1.5 Casting and Ranges of Variables
- Key Points: Casting converts a variable from one type to another. Be aware of the ranges for data types to avoid overflow or unexpected behavior.
- Sample Code:
20 Multiple-Choice Questions (MCQs)
- What is one of the primary reasons Java is widely used? A) It is the fastest programming language. B) It can only be run on Windows. C) It is platform-independent due to the JVM. D) It does not support object-oriented programming.
- Which data type would be best for storing a person's age? A) int B) double C) char D) boolean
- What will the following Java statement print?
System.out.println(6 + 9 * 2);
A) 24 B) 15 C) 30 D) 21
- What is the result of the following compound assignment?
int a = 5; a *= 3;
A) 8 B) 15 C) 5 D) 18
- What will be the output of this Java code?
double a = 100.234; int b = (int) a; System.out.println(b);
A) 100.0 B) 100 C) 101 D) 100.234
- What does the
==
operator check in Java? A) If two variables have the same type. B) If two variables store the same value. C) If two variables point to the same memory location. D) If two variables are constants.
- What will happen if you try to store the number 256 in a byte variable in Java? A) It will compile and run without issue. B) It will cause a runtime error. C) It will not compile. D) The variable will wrap around to a negative value.
- What does the following code snippet output?
int x = 5; int y = x++; System.out.println(y);
A) 5 B) 6 C) 10 D) 0
- Which of the following is not a primitive data type in Java? A) int B) String C) boolean D) char
- What will this code output?
int a = 9; int b = a--; System.out.println(b);
A) 9 B) 8 C) 10 D) 0
- How do you declare a constant in Java? A) int const a = 10; B) final int a = 10; C) static int a = 10; D) constant int a = 10;
- What will be the result of the following expression in Java?
15 + 3 * (4 / 2)
A) 24 B) 18 C) 21 D) 15
- What is the output of the following Java code?
double x = 10.5; double y = x % 3; System.out.println(y);
A) 1.5 B) 3.5 C) 2.5 D) 4.5
- Which operator is used to compare two variables for inequality in Java? A) = B) == C) != D) <=
- What is the default value of a boolean variable in Java? A) true B) false C) 0 D) null
- Which keyword is used to create a new instance of an object in Java? A) struct B) new C) class D) this
- What does the
&&
operator signify in Java? A) OR B) AND C) NOT D) XOR
- What is the range of values that can be assigned to a short variable in Java? A) -32768 to 32767 B) -128 to 127 C) 0 to 65535 D) -2147483648 to 2147483647
- What will the following code print?
int a = 10; int b = ++a; System.out.println(a + " " + b);
A) 10 11 B) 11 11 C) 11 10 D) 10 10
- What type of error will occur if you try to use an undeclared variable in Java? A) Syntax error B) Runtime error C) Logical error D) Compilation error
Answers to MCQs
- C
- A
- A
- B
- B
- B
- C
- A
- B
- A
- B
- C
- A
- C
- B
- B
- B
- A
- B
- D
- Minimum: The most negative value in two's complement for 16 bits is -32768.
- Maximum: The most positive value for 16 bits is 32767.
- Pre-Increment (
++a
): This operator increments the value ofa
first before the value is used in any expression. For example, ifa
is 5 and you use++a
in an expression or print statement,a
becomes 6 first, and then 6 is used in the expression or output. - Post-Increment (
a++
): This operator increments the value ofa
after the current value has been used in the expression. Ifa
is 5 and you usea++
in an expression, the value 5 is used first, and thena
is incremented to 6 afterward.
q18 in details:
In Java, the
short
data type is a 16-bit signed two's complement integer. This means it can hold values from -32768 to 32767.Here's the breakdown:
Therefore, the correct answer is:
A) -32768 to 32767
a++ Vs ++a
In Java,
++a
(pre-increment) and a++
(post-increment) are both increment operators that increase the value of variable a
by 1. However, they differ in how they return the value in expressions:Example:
Example:
The key difference lies in when the increment operation takes place relative to the value being used in the expression. This distinction can affect the outcome of the code depending on how these operators are used.
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/csaunit1
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章