Courseiva
Data Types, Variables, Basic I/O and OperatorshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A network engineer uses bitwise operators to set flags for packet filtering. The variable 'flags' currently holds the integer 10 (binary 1010). To enable the second bit (value 2) and disable the fourth bit (value 8), which expression should be used?

⚠ Common exam trap

Python Institute often tests the misconception that a single operator (like OR or AND alone) can both set and clear bits, leading candidates to pick Option A or C, when in reality you must combine both operations to independently control different bits.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

flags = (flags | 2) & ~8

It combines two operations in a single expression: first, it sets the second bit (value 2) using the bitwise OR (|) operator, which turns on that bit without affecting others; second, it clears the fourth bit (value 8) using the bitwise AND with the complement of 8 (& ~8), which forces that bit to 0. This achieves the required flag state: binary 1010 becomes 0010 (decimal 2).

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • flags = flags | 2

    Why it's wrong here

    Only sets the second bit, does not clear the fourth

  • flags = flags ^ 10

    Why it's wrong here

    XOR toggles bits, not the desired operation

  • flags = flags & ~8

    Why it's wrong here

    Only clears the fourth bit, does not set the second

  • flags = (flags | 2) & ~8

    Why this is correct

    Correctly sets bit 1 and clears bit 3

About these practice questions

This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.