IB Computer Science B2.1: Programming fundamentals

New IB Computer Science syllabus · first exams May 2027 · Theme B · Paper 2 · SL & HL · Python and Java

Guiding question: How can we apply computer programming to solve problems?

B2 Programming is the largest unit in the course (40 hours SL, 42 hours HL) and the core of Paper 2, which is written in real code, in Python or Java. B2.1 covers the basics every program needs.

StatementTopicCommand term
B2.1.1Global and local variables of various data typesConstruct / Trace
B2.1.2Extracting and manipulating substringsConstruct
B2.1.3Exception handlingDescribe
B2.1.4Debugging techniquesConstruct / Use

Key points

B2.1.1 Variables and data types

  • The five types named in the guide:
    • Boolean
    • char: a single character in Java; a 1-character string in Python
    • decimal: float in Python, double in Java
    • integer
    • string
  • Scope:
    • Local variables exist only inside their function.
    • Global variables are visible everywhere. In Python, you must write global inside a function to change one.
  • Be ready to trace how values change across function calls.

B2.1.2 Substrings

Find, extract, alter, concatenate and replace parts of a string. Python uses slicing (s[a:b]) and find(); Java uses substring() and indexOf().

B2.1.3 Exception handling

  • Three failure points the guide names:
    • unexpected inputs
    • unavailable resources, such as a missing file
    • logic errors
  • Python: try / except / finally
  • Java: try / catch / finally

The finally block always runs, so use it to close resources and tidy up.

B2.1.4 Debugging

  • Trace tables
  • Breakpoints
  • Print statements
  • Stepping through the code line by line

Each one reveals where the actual values differ from what you expected.


Exam-style question

Construct code, in either Python or Java, that extracts the domain (the part after the "@") from the string "student@cswithrazia.com" and outputs it. [4]

Mark scheme

[2] for correct index logic, [2] for correct output.

Python

email = "student@cswithrazia.com"
domain = email[email.find("@") + 1:]
print(domain)   # cswithrazia.com

Java

String email = "student@cswithrazia.com";
String domain = email.substring(email.indexOf("@") + 1);
System.out.println(domain);   // cswithrazia.com

Common mistakes that cost marks

  • Off-by-one errors in substrings. The + 1 above skips the "@" itself.
  • Changing a global in Python without global. Python then creates a new local variable instead.
  • Writing except with no specific error, and no finally, when the question asks you to manage a resource such as a file.

Frequently asked questions

Can I answer Paper 2 in Python or Java?
Yes. Paper 2 is available in parallel Python and Java versions. Learn one language thoroughly.

Do I need both try/except and try/catch?
You need the one for your language: try/except in Python, or try/catch in Java, plus the finally block in either.


Go further