Java Programming Concepts Q&A, Exams of Biology

A series of questions and answers covering fundamental concepts in java programming. It includes topics such as object-oriented programming, java history, program structures, paradigms, objects vs. Classes, constructor methods, instances of classes, and various elements of java syntax and data types. The questions address key aspects of java, making it a useful resource for students learning the language. It also covers methods, variables, and other essential components of java programming, offering a concise overview of the language's core principles. Useful for exam preparation and quick revision of java concepts.

Typology: Exams

2025/2026

Available from 10/24/2025

tutor-lee-1
tutor-lee-1 🇺🇸

4.3

(3)

11K documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI21 Java fully solved
2025(graded A+)
Java Programming Language - answer - Object-oriented
programming language
- Platform independent
Java History - answer 1. 1991 - Sun Microsystems developed
language for consumer electronic devices
2. 1993 - WWW increased need for "dynamic" web pages
3. 1995 - Sun formally announces Java for Web use
Types of Java Programs - answer 1. Applications
- General-purpose programs
- Standalone
- Executed through OS
2. Applets (outdated and deprecated)
- Programs meant for WWW
- Embedded in Web page
- Normally executed through browser
Java Program Structure - answer 1. Java Program
- (optional) import declarations
- Class declaration
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Java Programming Concepts Q&A and more Exams Biology in PDF only on Docsity!

CSCI21 Java fully solved

2025(graded A+)

Java Programming Language - answer - Object-oriented programming language

  • Platform independent Java History - answer 1. 1991 - Sun Microsystems developed language for consumer electronic devices
  1. 1993 - WWW increased need for "dynamic" web pages
  2. 1995 - Sun formally announces Java for Web use Types of Java Programs - answer 1. Applications
  • General-purpose programs
  • Standalone
  • Executed through OS
  1. Applets (outdated and deprecated)
  • Programs meant for WWW
  • Embedded in Web page
  • Normally executed through browser Java Program Structure - answer 1. Java Program
  • (optional) import declarations
  • Class declaration
  1. Class
  • Class name match file name
  • May extend an existing class (e.g. Jcomponent)
  • Contains fields and methods Paradigm - answer - Collection of interacting objects
  • Programs we specify are in these objects and how these objects behave Object - answer Something that has BAIT:
  1. Behavior - methods (things that obj
  2. Attributes - set of properties (field) w/ diff. values
  3. Identity - distinct instance of a class of obj
  4. Type - belongs to a class of similar objects Object vs. Class - answer Object - instance of a class, physical entity employee obj. = new employee(); Class - group of similar objects (blueprint of objects) class employee { }
  • Used for classes, variables, methods, etc. How are identifiers typed? - answer - Consists of letters, digits, $, _
  • Should start with a letter or underscore, NO spaces Ex. Balance, score5, total_credit, _one4Three
  • Some identifiers are reserved words Java Conventions - answer 1. Class Names - capitalize 1st letter of every word
  1. Variable & Method Names - camelCase Java Data Types - answer 1. Primitive - boolean, char, byte, short, int, long, float (7) and double (15)
  2. Non-Primitive - Classes, Interfaces, and Arrays. Methods - answer - Describes specific behavior applicable to objects of a class
  • Defines set of instructions/statements to be executed when called
  • Called or invoked on object of class How to call a method - answer b.deposit(500); name of object.method(parameter);

Method Composition - answer Signature: public void deposit( double amount ) (parameter) Body statements/instructions inside { } Method Intents - answer 1. Mutator - modify an object's state

  1. Accessor - return some info about the object Mutator - answer - Indicates a void return type (no value returned)
  • Has parameters
  • Instance fields are updated within method EX. public void deposit ( double amount ) Accessor - answer - Return type
  • If not, values are displayed as output
  • No parameters Ex. public double getBalance()
  1. Implement methods by specifying statements
  • Statements will access instance fields
  1. Test class
  • Write tester program that makes objects & invokes methods Order of Declarations - answer - May come in ANY order Most common order:
  1. Instance fields
  2. Constructors
  3. Methods Statements - answer - Body of a method contains sequence of statements
  • End with ; EX. return, print/output, method calls, assignments (balance = 0); Format Specifier Syntax - answer System.out.printf( "pi = %5.3f\n", pi); % - start of specifier 5 - width of no. is 5 .3 - 3 decimal places f - conversion character (float) \n - line break

Components of Primitive Data Types - answer 1. Range of values

  1. Literals (how constant values under type are written)
  2. Operations that can be carried out w/ values under that type Int - answer - int can be double but not vice versa
  • Cannot do int = d Double - answer - Limited to 64 bits or 8 bytes Operations
  • Arithmetic operations with true division (as in decimals included, not just removed like in int division)
  • Float: lower precision (fewer digits) Float - answer - For double and float types, value stored or computed is often not exact
  • Binary storage & precision constraints, values are rounded Float can be double but not vice versa Char - answer - Characters on keyboard or printed on output device
  • Literals: 'A', 'b', '5', '\n', '\u0009'
  • Values come from Unicode character set (subsumes ASCII character set)
  • Static methods have access to class variables (static variables) w/o using the class's object (instance)
  • Only static data may be accessed by a static method.
  • built-in object in java Expressions - answer - Sequence of variables, literals, operators, and method calls (EX. Math.pow() )
  • Has return value and type EX. int score; score = 90; // this is the expression Casting - answer - used to "force" conversion
  • for converting doubles/ floats to longs/ints Syntax: (type) expression EX. Int x = (int) 5.65; Rounding - answer - Math.round() to round floating point numbers to whole numbers EX. Float f = 5.65; Double d = 4.44; Int i = Math.round(f); // assigns 6 Long l = Math.round(d); // assigns 4

Increment and Decrement - answer A++ and ++a has same effect

  • but a++ is post-increment
  • and ++a is pre-increment Post-Increment Operator ++ - answer - Returns original value of operand
  • Assignment then increment
  • Variable is incremented AFTER value is returned Pre-Increment Operator ++ - answer - Returns NEW (incremented) value of operand
  • Increment then assignment
  • Variable is incremented BEFORE value is returned Constants - answer - Literal values in program
  • May be associated w/ appropriate name
  • Declare at method level after { for the class Primitives VS Objects - answer Primitive - directly has values Objects - contain references to values (points) Null Reference - answer Object variable = null Means variable currently isn't referring to object EX. B = null;

Methods on string objects - answer - Javap java.lang.String for complete list

  • Public int length()
  • Public String toUpperCase()
  • Public String substring( int first, int last ) String Concatenations - answer System.out.println("basket"+"ball"); (+) operator causes concatenation if all operands Strings System.out.println("The ans is " + ans); If only 1 is string, other is converted to string then concatenated Converting Strings to Numbers - answer Integer.parseInt(s); Double.parseDouble(s); Converting Number to String - answer Integer.toString(i); Double.toString(d); Substring Syntax - answer public String substring(int first, int last-

String s = "Ateneo de manila"; String a = s.substring(0, 6); // "Ateneo" String b = s.substring(6, 9); // " de"

Substring Method - answer - Each character in string object has a position (starting 0) Parameters for substring indicate:

  • First - starting letter of substring
  • Last - position ff ending letter of substring
  • (last-first) = length of resulting substring Input Scanner syntax - answer import java.util.Scanner; Scanner in = new Scanner(System.in); int i = in.nextInt( ); double d = in.nextDouble( ); String s = in.nextLine( ); // reads entire line String w = in.next( ); // reads 1 word Conditional Execution - answer - If statement
  • Boolean variables, operators, expressions, and methods -? Operator
  • Switch statement Block Statements - answer Block allows grouping of statements into 1
  • Place statements in sequence
  • Surround w curly braces
  • uppercase comes before lowercase (Hello comes before car) Comparing Objects - answer - Object variables contain references == tests for identity, not for object content
  • Object references aren't same even if contents are same Testing for Null - answer - Null reference = no object
  • Use ==, not equals ( ), to test for null
  • Null isn't the same as empty String " " If-Else Chain - answer - Statement that corresponds to 1st matching condition is executed
  • Order matters When to use switch statements? - answer If condition in if-else chain are exact comparisons (==) on integral (char) values If conditions in if-else chain are designed to check for constant values representing a finite number of cases Switch-Case Statement - answer Switch (exp)
  • "Countable" primitive type (Byte, int, char, NOT float or double) Case literal
  • "Entry point" label for case when exp == literal Break
  • Causes exit block
  • If no break, control "falls through" Boolean Data Type - answer true or false (lowercase) Operations
  • Relational operators ( <, >, etc)
  • Logical operators (&&, ||, !) Boolean Variables - answer - Convenient for long conditions
  • Variable w/ a condition resulting to a boolean Methods with Boolean Return Values - answer - Allows encapsulation of complex conditions, or conditions that depend on state of object EX. isBelowMin( ) Dangling Else - answer - Else clause matches the nearest enclosing if
  • Use { } to match the outer if if (condition) if (condition) else // else matches to the 2nd if, not the first ? Operator Syntax - answer condition? true value : false value
  1. Terminating / continuing condition
  2. Incrementing step (smth that moves the program) EX. Decrement, divide, multiply, get another input
  3. Loop body For or While? Which to use? - answer For - when number of iterations is known While - unknown number of iterations, check condition first Do-While - unknown number of iterations but done at least once Loop Pitfalls - answer 1. Infinite Loops
  4. Real Numbers
  5. Off-By-One-Error (OBOE) aka "fencepost error" Loop Pitfall - Real Numbers - answer - Avoid == or != when using real numbers
  • Use <= or >= depending on direction (counting up or down) of the loop Off-By-One-Error (OBOE) aka "fencepost error" - answer Execute loop body N times
  • If counter starts at 0, counter < N
  • If counter starts at 1, counter <= N After the loop, counter (if still in scope) will be beyond the limit of the condition
  • If counter starts at 0, counter = N
  • If counter starts at 1, counter = N + 1 Class Relationships - answer - More complex programs require multiple classes
  • Objects have fields that refer to other objects EX. In class A, there can be a field whose type is class B i.e. there's a class relationship between A and B Types of Class Relationships - answer 1. Composition or aggregation
  1. Association Object Composition / Aggregation - answer Parent-child relationship (child cannot exist without parent)
  • Objects can be composed of other objects
  • Have references to "parts" of class as fields of class
  • Objects can make instances of other objects Encapsulation - answer - "hiding" implementation details
  • Data hidden w/ the object that it belongs to
  • Data changes done via methods of object that contains data
  • Objects know how to handle data
  • Allows object's programmer to change data representation so fields are private